<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Joy Kareko</title>
    <description>The latest articles on DEV Community by Joy Kareko (@joy_kareko_1cc956e11d827c).</description>
    <link>https://dev.to/joy_kareko_1cc956e11d827c</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4088473%2F4da266e6-67bf-4591-b8c5-182e8878b212.jpg</url>
      <title>DEV Community: Joy Kareko</title>
      <link>https://dev.to/joy_kareko_1cc956e11d827c</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/joy_kareko_1cc956e11d827c"/>
    <language>en</language>
    <item>
      <title>Why Your AI Agent Keeps Remembering Its Own Notes: Fixing Memory Pollution in AWS AgentCore</title>
      <dc:creator>Joy Kareko</dc:creator>
      <pubDate>Tue, 01 Dec 2026 00:13:00 +0000</pubDate>
      <link>https://dev.to/joy_kareko_1cc956e11d827c/why-your-ai-agent-keeps-remembering-its-own-notes-fixing-memory-pollution-in-aws-agentcore-dle</link>
      <guid>https://dev.to/joy_kareko_1cc956e11d827c/why-your-ai-agent-keeps-remembering-its-own-notes-fixing-memory-pollution-in-aws-agentcore-dle</guid>
      <description>&lt;p&gt;You add memory to your AI agent with Amazon Bedrock AgentCore Memory. It works. The agent remembers a customer's name, their last order and what they asked about yesterday.&lt;/p&gt;

&lt;p&gt;A few days later, things get strange. Answers start repeating old facts. The context the agent pulls back keeps getting longer. Some "memories" look like notes the agent wrote to itself.&lt;/p&gt;

&lt;p&gt;Nothing crashed, so it's easy to miss. But your agent's memory is slowly filling up with its own echoes.&lt;/p&gt;

&lt;p&gt;I call this &lt;strong&gt;memory pollution&lt;/strong&gt;, and it comes from one small mistake that's very easy to make. Let's look at what it is, how to spot it and how to fix it.&lt;/p&gt;

&lt;p&gt;In this post:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How memory usually gets added to an agent&lt;/li&gt;
&lt;li&gt;The one line that causes memory pollution&lt;/li&gt;
&lt;li&gt;How to spot it&lt;/li&gt;
&lt;li&gt;How to fix it, and clean up afterwards&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How memory usually works in an agent
&lt;/h2&gt;

&lt;p&gt;A common pattern with AgentCore Memory and the Strands Agents SDK is to use &lt;strong&gt;hooks&lt;/strong&gt;. A hook is a function that runs automatically at a certain moment in the agent's life, for example when a request starts or when it ends.&lt;/p&gt;

&lt;p&gt;Most memory setups use two hooks:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;When a request starts:&lt;/strong&gt; look up related memories and add them to the user's message as extra context.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;When the request ends:&lt;/strong&gt; save the conversation turn to memory, so it can be recalled later.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;AgentCore keeps those saved turns as &lt;strong&gt;events&lt;/strong&gt;. If you've configured long-term memory strategies, AgentCore also processes the events in the background and extracts useful facts, such as "the customer prefers email" or "the customer's name is Amina". These extracted facts are stored separately as &lt;strong&gt;memory records&lt;/strong&gt;. Extraction happens asynchronously, so new records may not be available straight away.&lt;/p&gt;

&lt;p&gt;This is a good design. The problem is in how step 1 and step 2 interact.&lt;/p&gt;

&lt;h2&gt;
  
  
  The one line that causes it
&lt;/h2&gt;

&lt;p&gt;Here's a simplified version of a memory hook. It's a teaching example, so it leaves out error handling. Look closely at what gets saved.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;strands&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hooks&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;AfterInvocationEvent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;BeforeInvocationEvent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;HookProvider&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;HookRegistry&lt;/span&gt;


&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MemoryHook&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;HookProvider&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;memory_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;actor_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;session_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;
        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;memory_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;memory_id&lt;/span&gt;
        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;actor_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;actor_id&lt;/span&gt;
        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;session_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;session_id&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;add_context&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;BeforeInvocationEvent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;messages&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt;
        &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;messages&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"role"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="s2"&gt;"user"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt;

        &lt;span class="n"&gt;user_text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"content"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"text"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="n"&gt;memories&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;retrieve_memories&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;memory_id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;memory_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;namespace&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"/support/customer/{self.actor_id}/facts/"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;user_text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;top_k&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;context&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"\n"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"content"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"text"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;memories&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="c1"&gt;# The user's message is rewritten to include the memories&lt;/span&gt;
            &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"content"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"text"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"Relevant context:\n{context}\n\nUser: {user_text}"&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;save_turn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;AfterInvocationEvent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;messages&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;agent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;messages&lt;/span&gt;
        &lt;span class="n"&gt;user_text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;messages&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"content"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"text"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;  &lt;span class="c1"&gt;# &amp;lt;-- the problem&lt;/span&gt;
        &lt;span class="n"&gt;reply&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;messages&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"content"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"text"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create_event&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;memory_id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;memory_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;actor_id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;actor_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;session_id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;session_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;messages&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user_text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"USER"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;reply&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"ASSISTANT"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;register_hooks&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;registry&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;HookRegistry&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;registry&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;BeforeInvocationEvent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;add_context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;registry&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;AfterInvocationEvent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;save_turn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A note on the namespace: the one you retrieve from has to match the namespace configured in your memory strategy. AWS recommends a leading and trailing slash, as shown here.&lt;/p&gt;

&lt;p&gt;Did you spot the problem?&lt;/p&gt;

&lt;p&gt;&lt;code&gt;add_context&lt;/code&gt; &lt;strong&gt;changes the user's message in place&lt;/strong&gt;. By the time &lt;code&gt;save_turn&lt;/code&gt; runs, the "user message" in the conversation isn't what the user typed anymore. It's the user's words &lt;em&gt;plus&lt;/em&gt; every memory that was retrieved.&lt;/p&gt;

&lt;p&gt;So the agent saves its own memories back into memory, as if the user had said them.&lt;/p&gt;

&lt;p&gt;Here's what that looks like in storage. This is what gets saved as the USER message:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Relevant context:
The customer's name is Amina.

User: Where's my order?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And this is what &lt;em&gt;should&lt;/em&gt; be saved:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Where's my order?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Why this snowballs
&lt;/h2&gt;

&lt;p&gt;Here's what happens over a few turns:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Turn 1: the user says "Hi, I'm Amina." That gets saved. AgentCore extracts the fact: the customer's name is Amina.&lt;/li&gt;
&lt;li&gt;Turn 2: the user asks "Where's my order?" The hook adds "the customer's name is Amina" to the message. The &lt;strong&gt;whole thing&lt;/strong&gt; gets saved.&lt;/li&gt;
&lt;li&gt;AgentCore processes that saved turn and extracts the name fact again, now from a message that looks like the user said it.&lt;/li&gt;
&lt;li&gt;Turn 3: more memories are retrieved, added, and saved again.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Each turn feeds old memories back in. You get duplicate facts, longer and longer context, and old information that keeps getting reinforced even after it's out of date. If a fact was wrong once, it keeps coming back.&lt;/p&gt;

&lt;p&gt;There can also be a cost. More text saved means more text processed, and more context sent to the model on every turn.&lt;/p&gt;

&lt;p&gt;There's a second, smaller bug hiding in &lt;code&gt;save_turn&lt;/code&gt;. It assumes the second-to-last message is always the user's input, and that the last message is the reply. That assumption can break when the conversation includes tool calls, tool results or other messages. It's safer to identify the user's message and the reply explicitly than to rely on their position in the list.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to spot it
&lt;/h2&gt;

&lt;p&gt;You don't need special tools. Check what's actually stored, in both places:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Raw events.&lt;/strong&gt; List the events for one test customer. If a USER message starts with &lt;code&gt;Relevant context:&lt;/code&gt; (or whatever label your hook uses), you have memory pollution.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Long-term memory records.&lt;/strong&gt; Check these separately. If the same fact appears again and again with slightly different wording, that's another sign.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Context size.&lt;/strong&gt; Log the length of the context your hook retrieves. If it keeps growing for the same customer, something is feeding back in.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The fix: save what the user actually said
&lt;/h2&gt;

&lt;p&gt;The fix is simple. Keep a copy of the original message &lt;em&gt;before&lt;/em&gt; you add context, and save that copy.&lt;/p&gt;

&lt;p&gt;Where you keep the copy matters. If you store it on the hook object itself, two requests running at the same time could overwrite each other's copy. Strands passes an &lt;code&gt;invocation_state&lt;/code&gt; dictionary through each request, and it's available to both hooks. That's the right place for anything that belongs to one request.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;strands&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hooks&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;AfterInvocationEvent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;BeforeInvocationEvent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;HookProvider&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;HookRegistry&lt;/span&gt;


&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MemoryHook&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;HookProvider&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;memory_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;actor_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;session_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;
        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;memory_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;memory_id&lt;/span&gt;
        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;actor_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;actor_id&lt;/span&gt;
        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;session_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;session_id&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;add_context&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;BeforeInvocationEvent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;messages&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt;
        &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;messages&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"role"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="s2"&gt;"user"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt;

        &lt;span class="n"&gt;user_text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"content"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"text"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="c1"&gt;# Keep the clean version for this request only&lt;/span&gt;
        &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;invocation_state&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"original_user_text"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;user_text&lt;/span&gt;

        &lt;span class="n"&gt;memories&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;retrieve_memories&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;memory_id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;memory_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;namespace&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"/support/customer/{self.actor_id}/facts/"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;user_text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;top_k&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;context&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"\n"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"content"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"text"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;memories&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"content"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"text"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"Relevant context:\n{context}\n\nUser: {user_text}"&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;save_turn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;AfterInvocationEvent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;user_text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;invocation_state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"original_user_text"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;user_text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt;

        &lt;span class="c1"&gt;# This hook also runs when a request fails, so only save finished replies&lt;/span&gt;
        &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stop_reason&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="s2"&gt;"end_turn"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt;

        &lt;span class="n"&gt;reply&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;strip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;reply&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt;

        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create_event&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;memory_id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;memory_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;actor_id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;actor_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;session_id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;session_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;messages&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user_text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"USER"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;reply&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"ASSISTANT"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;register_hooks&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;registry&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;HookRegistry&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;registry&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;BeforeInvocationEvent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;add_context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;registry&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;AfterInvocationEvent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;save_turn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What changed:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The original message is kept in &lt;code&gt;invocation_state&lt;/code&gt;&lt;/strong&gt;, so each request has its own copy.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;save_turn&lt;/code&gt; saves that clean copy&lt;/strong&gt;, not the rewritten message.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The reply comes from the request's result&lt;/strong&gt;, not from a position in the message list, so tool calls don't break it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Failed requests aren't saved.&lt;/strong&gt; The end-of-request hook runs whether the request succeeded or failed, so the code checks that it finished normally before saving anything.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The agent still gets the extra context while it thinks. It just doesn't write that context back into memory.&lt;/p&gt;

&lt;h3&gt;
  
  
  Another option: don't touch the user's message at all
&lt;/h3&gt;

&lt;p&gt;If you'd rather avoid the problem completely, put the retrieved memories somewhere other than the user's message, for example in the system prompt for that turn. Then there's nothing to undo when you save. Both approaches work. The important rule is the same: &lt;strong&gt;memory should store what was said, not what you added.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Before you use this in production
&lt;/h3&gt;

&lt;p&gt;This is still a teaching example. The hook events used here are documented in the Strands SDK, but check them against the version you have installed. A production version also needs to handle messages that aren't plain text (images, documents), structured outputs, and retries that could save the same turn twice.&lt;/p&gt;

&lt;h2&gt;
  
  
  Clean up after the fix
&lt;/h2&gt;

&lt;p&gt;Fixing the code stops new pollution, but it doesn't remove what's already stored.&lt;/p&gt;

&lt;p&gt;AgentCore keeps raw events and extracted long-term memory records separately. If a polluted event has already produced a memory record, deleting the event alone won't remove that record. You may need to delete both.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;For testing:&lt;/strong&gt; start with a fresh customer ID (actor ID). That gives you a clean memory to check the fix with.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;For existing users:&lt;/strong&gt; look at the affected events &lt;em&gt;and&lt;/em&gt; memory records, and clean up both where needed.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Check that it worked
&lt;/h2&gt;

&lt;p&gt;Run a simple two-session test with the same customer ID and different session IDs:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Session A:&lt;/strong&gt; "Hi, I'm Amina and I prefer email updates."&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Wait for extraction.&lt;/strong&gt; It happens in the background, so don't rely on a fixed delay. Check the memory records until the expected facts appear.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Session B:&lt;/strong&gt; "What's my name, and how should you contact me?"&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The agent should answer "Amina" and "email". Then look at your saved events: the USER messages should contain exactly what you typed, with no &lt;code&gt;Relevant context:&lt;/code&gt; anywhere.&lt;/p&gt;

&lt;h2&gt;
  
  
  Quick checklist
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The hook saves the user's original text, not the message with context added.&lt;/li&gt;
&lt;li&gt;The original text is kept per request (in &lt;code&gt;invocation_state&lt;/code&gt;), not on the hook object.&lt;/li&gt;
&lt;li&gt;The reply comes from the request's result, and failed requests aren't saved.&lt;/li&gt;
&lt;li&gt;Your retrieval namespace matches the one configured in your memory strategy.&lt;/li&gt;
&lt;li&gt;No saved USER message contains your context label.&lt;/li&gt;
&lt;li&gt;You checked both raw events and long-term memory records.&lt;/li&gt;
&lt;li&gt;You tested with a fresh customer ID.&lt;/li&gt;
&lt;li&gt;A two-session test recalls the right facts.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The takeaway
&lt;/h2&gt;

&lt;p&gt;Memory bugs are quiet. Nothing crashed, and the agent still seems to work. That's exactly why they're worth checking for.&lt;/p&gt;

&lt;p&gt;When you add context to help your agent think, make sure that context doesn't sneak back into what you save. Store what was said, not what you added.&lt;/p&gt;

&lt;p&gt;Have you run into a strange memory bug in your own agents? Tell me in the comments.&lt;/p&gt;

&lt;h2&gt;
  
  
  Further reading
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/memory-strategies.html" rel="noopener noreferrer"&gt;AgentCore Memory strategies&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/specify-long-term-memory-organization.html" rel="noopener noreferrer"&gt;Organising long-term memory with namespaces&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/short-term-delete-event.html" rel="noopener noreferrer"&gt;Deleting AgentCore Memory events&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/long-term-delete-memory-records.html" rel="noopener noreferrer"&gt;Deleting long-term memory records&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://strandsagents.com/docs/api/python/strands.hooks.events/" rel="noopener noreferrer"&gt;Strands hook events&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;I drafted this post with help from an AI assistant, then tested the code and edited it myself.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>aws</category>
      <category>ai</category>
      <category>agents</category>
    </item>
    <item>
      <title>How AWS OpenSearch gave me a surprise AWS bill, and how to avoid it</title>
      <dc:creator>Joy Kareko</dc:creator>
      <pubDate>Mon, 23 Nov 2026 13:08:50 +0000</pubDate>
      <link>https://dev.to/joy_kareko_1cc956e11d827c/how-aws-opensearch-gave-me-a-surprise-aws-bill-and-how-to-avoid-it-4amf</link>
      <guid>https://dev.to/joy_kareko_1cc956e11d827c/how-aws-opensearch-gave-me-a-surprise-aws-bill-and-how-to-avoid-it-4amf</guid>
      <description>&lt;p&gt;I was building a RAG agent on AWS: an AI agent that answers questions by searching my own documents first. I created an Amazon Bedrock Knowledge Base, clicked &lt;strong&gt;Quick create&lt;/strong&gt; for the vector store, tested a few questions and moved on to the next part of the project.&lt;/p&gt;

&lt;p&gt;I wasn't using the knowledge base much. A handful of test questions, then nothing.&lt;/p&gt;

&lt;p&gt;Then my AWS account hit its spending limit.&lt;/p&gt;

&lt;p&gt;The total wasn't huge, under $25. But it built up in a short time, on something I thought was sitting there doing nothing. When I traced it, almost all of it came from one service: &lt;strong&gt;Amazon OpenSearch Serverless&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Here's what happened, why it happens, and how to make sure it doesn't happen to you.&lt;/p&gt;

&lt;p&gt;In this post:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What "Quick create" sets up behind the scenes&lt;/li&gt;
&lt;li&gt;Why OpenSearch Serverless charges you even when nobody is searching&lt;/li&gt;
&lt;li&gt;How to find what's costing you money&lt;/li&gt;
&lt;li&gt;Five ways to avoid the surprise&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What Quick create actually sets up
&lt;/h2&gt;

&lt;p&gt;A Bedrock Knowledge Base needs somewhere to store your documents as &lt;strong&gt;vectors&lt;/strong&gt;: lists of numbers that capture the meaning of each piece of text, so the agent can find relevant passages by meaning rather than exact words. That storage is called a &lt;strong&gt;vector store&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;When you choose &lt;strong&gt;Quick create a new vector store&lt;/strong&gt; and pick OpenSearch Serverless, Bedrock creates an OpenSearch Serverless vector search collection and index for you, already set up with the fields it needs.&lt;/p&gt;

&lt;p&gt;That's convenient. The catch is that the collection is a separate resource in your account, with its own bill. And it doesn't bill the way most people expect from something called "serverless".&lt;/p&gt;

&lt;h2&gt;
  
  
  Why it charges you when nobody is searching
&lt;/h2&gt;

&lt;p&gt;OpenSearch Serverless measures computing power in &lt;strong&gt;OCUs&lt;/strong&gt; (OpenSearch Compute Units). You pay for OCUs by the hour.&lt;/p&gt;

&lt;p&gt;I assumed "serverless" meant "free when idle". For &lt;strong&gt;Classic&lt;/strong&gt; collections, which is the kind I had, that's not the case:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When you create your first collection, OpenSearch Serverless starts a set of OCUs, some for adding data (indexing) and some for answering searches.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Those OCUs keep running even when there's no activity on the collection.&lt;/strong&gt; They're the minimum capacity, and you're billed for them every hour.&lt;/li&gt;
&lt;li&gt;With the default high-availability setting (redundancy on), the minimum is four half-OCUs: two for indexing and two for search. At the listed rate in US East (N. Virginia), AWS puts that at approximately &lt;strong&gt;$350 a month&lt;/strong&gt;, before storage.&lt;/li&gt;
&lt;li&gt;Later collections in the same account can share those OCUs, so the minimum mostly hits you on your first collection.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;At that example rate, it's roughly 48 cents an hour, around the clock, whether you send one question or none. On a small test account with a spending limit, that runs out fast.&lt;/p&gt;

&lt;p&gt;AWS has since released &lt;strong&gt;NextGen&lt;/strong&gt; collections, which can scale to zero when idle. When I wrote this, AWS's own blog said NextGen collections weren't yet compatible with the Bedrock Knowledge Bases Retrieve API, and developers were reporting errors when they tried it. Check the current Bedrock documentation before you choose. Either way, find out which kind of collection you have, because Classic collections still carry that minimum.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to find what's costing you money
&lt;/h2&gt;

&lt;p&gt;If you're seeing charges you don't recognise, here's where to look:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open &lt;strong&gt;Billing and Cost Management → Cost Explorer&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Group by &lt;strong&gt;Service&lt;/strong&gt;, and set the date range to the last 7 days with daily detail.&lt;/li&gt;
&lt;li&gt;Look for &lt;strong&gt;Amazon OpenSearch Service&lt;/strong&gt;. Serverless charges appear under it.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Billing data isn't instant. It can lag by hours, so don't assume a service is free just because today's charges haven't shown up yet.&lt;/p&gt;

&lt;p&gt;Then check whether you have any collections still running:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;aws opensearchserverless list-collections
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If a collection is listed and you don't need it, it's still charging you.&lt;/p&gt;

&lt;h2&gt;
  
  
  Five ways to avoid the surprise
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Consider S3 Vectors for learning projects
&lt;/h3&gt;

&lt;p&gt;When you quick-create a knowledge base, OpenSearch Serverless isn't your only choice. &lt;strong&gt;Amazon S3 Vectors&lt;/strong&gt; is also an option. Bedrock creates an S3 vector bucket for you during knowledge base creation.&lt;/p&gt;

&lt;p&gt;S3 Vectors is storage-based, so there are no always-on compute units ticking over in the background. It isn't free: you still pay for storage and requests. It also has trade-offs, such as no hybrid search (combining keyword and meaning-based search) and tighter limits on metadata.&lt;/p&gt;

&lt;p&gt;For learning projects and RAG workloads that don't need very low latency or hybrid search, S3 Vectors can be a more cost-effective starting point. Move to OpenSearch when you actually need its speed and features.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. If you need OpenSearch, turn off redundancy for dev and test
&lt;/h3&gt;

&lt;p&gt;If you create the collection yourself, you can disable redundant replicas for development work. That lowers the minimum from four half-OCUs to two (one for indexing and one for search), which halves the idle cost to roughly $175 a month at the same example rate.&lt;/p&gt;

&lt;p&gt;Keep redundancy on for production, where you need high availability.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Delete the collection, not just the knowledge base
&lt;/h3&gt;

&lt;p&gt;This one catches a lot of people. The knowledge base and the OpenSearch collection are separate resources, and deleting the knowledge base can leave the vector store behind. When you're finished:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Delete the knowledge base.&lt;/li&gt;
&lt;li&gt;Check for the OpenSearch Serverless collection it used.&lt;/li&gt;
&lt;li&gt;Delete the collection if you no longer need it.&lt;/li&gt;
&lt;li&gt;Check for anything else the project created, such as S3 buckets and IAM roles.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;aws opensearchserverless list-collections
aws opensearchserverless delete-collection --id &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;collection-id&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also do this in the console under &lt;strong&gt;Amazon OpenSearch Service → Serverless → Collections&lt;/strong&gt;. If you use NextGen collection groups, check the group's capacity settings as well.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Set up a budget alert before you build
&lt;/h3&gt;

&lt;p&gt;AWS Budgets can email you when spending reaches an amount you choose:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open &lt;strong&gt;Billing and Cost Management → Budgets → Create budget&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Choose a &lt;strong&gt;monthly cost budget&lt;/strong&gt; with a small amount, for example $10.&lt;/li&gt;
&lt;li&gt;Add alerts at 50% and 80% of the amount, and add your email address.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Be clear about what this does. A budget alert &lt;strong&gt;warns&lt;/strong&gt; you; it doesn't shut anything down. It gives you an earlier warning so you can investigate before the charges grow. (You can set up budget actions that respond automatically, but they need extra setup, and they won't stop every kind of resource.)&lt;/p&gt;

&lt;p&gt;In my case, what stopped the charges was a spending limit already set on the account I was using, not anything I had configured. On a normal AWS account there's no such limit by default, so set up a budget alert &lt;strong&gt;before&lt;/strong&gt; you start a project, not after the bill arrives.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Tear down at the end of every session
&lt;/h3&gt;

&lt;p&gt;When you're learning, it's tempting to leave everything running "for tomorrow". With always-on services like this, that's exactly how small charges add up.&lt;/p&gt;

&lt;p&gt;Get into the habit of a quick teardown at the end of each session: delete the collection, or the whole project stack if you used infrastructure as code. If your setup is scripted, rebuilding it the next day is quick. An idle Classic collection costs money every hour.&lt;/p&gt;

&lt;h2&gt;
  
  
  Quick checklist
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;You know which vector store your knowledge base uses, and whether an OpenSearch collection is Classic or NextGen.&lt;/li&gt;
&lt;li&gt;You considered S3 Vectors for a learning project, or turned off redundancy for a dev OpenSearch collection.&lt;/li&gt;
&lt;li&gt;A budget alert is set up, with email notifications, and you know it warns rather than stops charges.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;aws opensearchserverless list-collections&lt;/code&gt; shows nothing you've forgotten about.&lt;/li&gt;
&lt;li&gt;You delete the collection, not just the knowledge base, when you're done.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The takeaway
&lt;/h2&gt;

&lt;p&gt;OpenSearch Serverless isn't a bad choice. Classic collections just have an idle cost, while NextGen and S3 Vectors bill in different ways, each with its own trade-offs.&lt;/p&gt;

&lt;p&gt;The real lesson is broader: when a convenient wizard creates cloud resources for you, you still need to understand what it created and how that resource bills. "Serverless" doesn't always mean "free when idle".&lt;/p&gt;

&lt;p&gt;My surprise was small because a spending limit stopped it. Without that limit, the same collection would have kept charging every hour until I noticed.&lt;/p&gt;

&lt;p&gt;Have you been caught out by an AWS service that kept charging when idle? Share it in the comments so others can avoid it too.&lt;/p&gt;

&lt;h2&gt;
  
  
  Further reading
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://docs.aws.amazon.com/bedrock/latest/userguide/knowledge-base-create.html" rel="noopener noreferrer"&gt;Create a knowledge base in Amazon Bedrock&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.aws.amazon.com/opensearch-service/latest/developerguide/serverless-scaling.html" rel="noopener noreferrer"&gt;Managing capacity limits for Amazon OpenSearch Serverless&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.aws.amazon.com/AmazonS3/latest/userguide/s3-vectors-bedrock-kb.html" rel="noopener noreferrer"&gt;Using S3 Vectors with Amazon Bedrock Knowledge Bases&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://aws.amazon.com/opensearch-service/pricing/" rel="noopener noreferrer"&gt;Amazon OpenSearch Service pricing&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;I drafted this post with help from an AI assistant, then checked the facts and edited it myself.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>aws</category>
      <category>opensearch</category>
      <category>vectorstores</category>
    </item>
    <item>
      <title>Fixing X-Ray Service Maps for Multi-Agent Systems on AWS</title>
      <dc:creator>Joy Kareko</dc:creator>
      <pubDate>Fri, 13 Nov 2026 19:30:38 +0000</pubDate>
      <link>https://dev.to/joy_kareko_1cc956e11d827c/fixing-x-ray-service-maps-for-multi-agent-systems-on-aws-3glc</link>
      <guid>https://dev.to/joy_kareko_1cc956e11d827c/fixing-x-ray-service-maps-for-multi-agent-systems-on-aws-3glc</guid>
      <description>&lt;p&gt;I built a multi-agent system on Amazon Bedrock AgentCore. An orchestrator agent takes each request and hands the work to specialist worker agents: one checks orders, one handles refunds, one writes the reply to the customer. Some workers also search knowledge bases, a few of them in parallel.&lt;/p&gt;

&lt;p&gt;Every agent worked. But the first time I opened the X-Ray service map, it showed the orchestrator, a box for Bedrock, and nothing else. No workers. No knowledge bases. Just a map that made my system look far simpler than it was.&lt;/p&gt;

&lt;p&gt;Getting a map that matched the real call chain took me much longer than building the agents. This post is everything I wish I'd known before I started.&lt;/p&gt;

&lt;p&gt;In this post:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What a service map is, and how X-Ray decides what to draw&lt;/li&gt;
&lt;li&gt;The basic setup&lt;/li&gt;
&lt;li&gt;Why worker agents and knowledge bases go missing, and how to fix it&lt;/li&gt;
&lt;li&gt;Why parallel calls break the trace&lt;/li&gt;
&lt;li&gt;How to check it worked&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  First, some quick definitions
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;trace&lt;/strong&gt; is the story of one request. When a customer asks for a refund, the trace records everything that happens because of it: the model calls, the tool calls and the work each agent does.&lt;/p&gt;

&lt;p&gt;A trace is made of &lt;strong&gt;spans&lt;/strong&gt;. A span is one step in the story, such as "asked the refund agent" or "searched the policy knowledge base". Each span records how long it took and whether it failed.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;service map&lt;/strong&gt; turns traces into a picture. Each service is a box (a &lt;strong&gt;node&lt;/strong&gt;), and each call between them is a line. For a multi-agent system, it's the quickest way to see which agent is slow, which one failed, and whether a request took the route you expected.&lt;/p&gt;

&lt;p&gt;Here's the part that caught me out: &lt;strong&gt;the map only draws what your traces describe as separate services.&lt;/strong&gt; If your code doesn't say "this is a call to a different thing", X-Ray has no reason to draw a separate box for it.&lt;/p&gt;

&lt;h2&gt;
  
  
  The basic setup
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Turn on CloudWatch Transaction Search (once per account)
&lt;/h3&gt;

&lt;p&gt;AgentCore sends traces through CloudWatch. Nothing appears until Transaction Search is on. You only do this once per AWS account and region.&lt;/p&gt;

&lt;p&gt;In the CloudWatch console:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Go to &lt;strong&gt;Application Signals (APM)&lt;/strong&gt; and choose &lt;strong&gt;Transaction search&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Choose &lt;strong&gt;Enable Transaction Search&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Tick the box to ingest spans as structured logs.&lt;/li&gt;
&lt;li&gt;Save.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Then give it about ten minutes before you expect to see anything.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Add OpenTelemetry to your agents
&lt;/h3&gt;

&lt;p&gt;AgentCore uses &lt;strong&gt;OpenTelemetry&lt;/strong&gt;, an open standard for recording traces, through the AWS Distro for OpenTelemetry (ADOT).&lt;/p&gt;

&lt;p&gt;The setup depends on where your agent runs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;On AgentCore Runtime:&lt;/strong&gt; the runtime has built-in observability and records basic runtime data for you. To also see the detailed spans from inside your agent (model calls, tool calls, AWS SDK calls), add ADOT to your &lt;code&gt;requirements.txt&lt;/code&gt;, and the runtime instruments your code when it starts. You don't need to run anything extra.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Anywhere else&lt;/strong&gt; (your laptop, a container, a VM): add ADOT too, and start the agent through the OpenTelemetry wrapper yourself.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;aws-opentelemetry-distro
strands-agents[otel]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you run the system on your own machine while testing, start it like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="kd"&gt;export&lt;/span&gt; &lt;span class="nv"&gt;AGENT_OBSERVABILITY_ENABLED&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="kd"&gt;export&lt;/span&gt; &lt;span class="nv"&gt;OTEL_PYTHON_DISTRO&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;aws_distro
&lt;span class="kd"&gt;export&lt;/span&gt; &lt;span class="nv"&gt;OTEL_PYTHON_CONFIGURATOR&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;aws_configurator

opentelemetry-instrument python agent.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Trace every request while you're developing
&lt;/h3&gt;

&lt;p&gt;Two separate settings decide whether you see a test request, and it's easy to mix them up:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Sampling&lt;/strong&gt; decides which requests are traced at all.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Indexing&lt;/strong&gt; decides which traces become searchable trace summaries. By default, Transaction Search indexes 1% of spans as trace summaries for free.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With a low sampling rate and only a handful of test requests, yours may never be traced, and it looks as if nothing happened.&lt;/p&gt;

&lt;p&gt;While you're developing, set your sampling rate to 100% so every request is captured. You can keep a lower indexing percentage if you like. Before production, review both settings, because tracing and indexing more data costs more.&lt;/p&gt;

&lt;p&gt;With all of this in place, you'll see traces. But you may still get the "orchestrator plus Bedrock" map I got. Here's why.&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem 1: Worker agents don't show up on the map
&lt;/h2&gt;

&lt;p&gt;A common multi-agent pattern is &lt;strong&gt;agents as tools&lt;/strong&gt;: each worker agent is wrapped in a tool, and the orchestrator calls the tool when it wants that worker's help.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nd"&gt;@tool&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;ask_refund_agent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="s2"&gt;"""Send refund requests to the refund agent."""&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;refund_agent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's a clean design. The catch is that the worker runs &lt;strong&gt;inside the orchestrator's process&lt;/strong&gt;. To the tracing library, it's just more code running in the same service. The worker's model calls show up, but they're filed under the orchestrator. There's no separate refund agent box.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The fix: wrap each worker call in its own named span, and mark it as a call to something downstream.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Two details matter:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The span's &lt;strong&gt;kind&lt;/strong&gt; is &lt;code&gt;CLIENT&lt;/code&gt;, which means "I'm calling something else".&lt;/li&gt;
&lt;li&gt;The span has a &lt;strong&gt;&lt;code&gt;peer.service&lt;/code&gt;&lt;/strong&gt; attribute naming the thing being called. X-Ray uses this name for downstream calls.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Be clear about what this does. Your workers are still running inside the orchestrator's process, so they're logical parts of one program rather than separate services. These spans make each worker call visible and named in the trace. In X-Ray they're recorded as downstream calls, which is usually what puts a separate, named box on the map. How exactly the map draws them depends on how X-Ray interprets the exported spans, so check the result against your own map.&lt;/p&gt;

&lt;p&gt;Here's a small helper:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;opentelemetry&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;trace&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;opentelemetry&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;trace&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;SpanKind&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Status&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;StatusCode&lt;/span&gt;

&lt;span class="n"&gt;tracer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;trace&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get_tracer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"orchestrator"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;traced_call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;node_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="s2"&gt;"""Run fn as a call to a separate node on the service map."""&lt;/span&gt;
    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;tracer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;start_as_current_span&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;node_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;kind&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nc"&gt;SpanKind&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;CLIENT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;attributes&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"peer.service"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;node_name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;span&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;exc&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;span&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;record_exception&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;exc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;span&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set_status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;StatusCode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;ERROR&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;raise&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And here's the tool using it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nd"&gt;@tool&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;ask_refund_agent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="s2"&gt;"""Send refund requests to the refund agent."""&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;traced_call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"refund-agent"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;lambda&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;refund_agent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Do the same for every worker, with a clear and different name for each one: &lt;code&gt;orders-agent&lt;/code&gt;, &lt;code&gt;refund-agent&lt;/code&gt;, &lt;code&gt;reply-agent&lt;/code&gt;. Each worker call now has its own name in the trace, and a failure is recorded against the right worker.&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem 2: All the knowledge bases merge into one box
&lt;/h2&gt;

&lt;p&gt;Knowledge base searches use the AWS SDK. When AWS SDK instrumentation is enabled (ADOT does this for &lt;code&gt;boto3&lt;/code&gt;), these calls can appear in your traces. But every search goes to the same AWS service, so they can be hard to tell apart. If you search three knowledge bases, you can't easily see which one is slow or failing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The fix is the same idea:&lt;/strong&gt; put a named span around each search, so you can see exactly which knowledge base was queried.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;bedrock_agent_runtime&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;boto3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;client&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"bedrock-agent-runtime"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;search_kb&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;kb_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;kb_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;traced_call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"KnowledgeBase:{kb_name}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="k"&gt;lambda&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;bedrock_agent_runtime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;retrieve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;knowledgeBaseId&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;kb_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;retrievalQuery&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"text"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now each knowledge base gets its own node, such as &lt;code&gt;KnowledgeBase:returns-policy&lt;/code&gt; and &lt;code&gt;KnowledgeBase:shipping-policy&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem 3: Parallel calls split into separate traces
&lt;/h2&gt;

&lt;p&gt;To save time, I searched several knowledge bases at once using a thread pool. Afterwards, the parallel searches didn't show up under the request at all. Some appeared as small traces of their own.&lt;/p&gt;

&lt;p&gt;Here's why. OpenTelemetry keeps track of "which trace am I in?" in something called the &lt;strong&gt;context&lt;/strong&gt;. A new thread starts with an empty context by default. So each search started a brand-new trace instead of joining the request's trace.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The fix: copy the context into each thread.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;contextvars&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;concurrent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;futures&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;ThreadPoolExecutor&lt;/span&gt;

&lt;span class="no"&gt;KNOWLEDGE_BASES&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"KB_ID_1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"returns-policy"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"KB_ID_2"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"shipping-policy"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"KB_ID_3"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"premium-benefits"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nc"&gt;ThreadPoolExecutor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;pool&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;futures&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="n"&gt;pool&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;submit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;contextvars&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copy_context&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;search_kb&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;kb_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;kb_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;kb_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;kb_name&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="no"&gt;KNOWLEDGE_BASES&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;future&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;result&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;future&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;futures&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;contextvars.copy_context()&lt;/code&gt; takes a copy of the current context, including the trace, and &lt;code&gt;.run&lt;/code&gt; starts the search inside that copy. Notice that the list makes a &lt;strong&gt;new copy for each task&lt;/strong&gt;. Keep it that way: don't make one copy and share it between threads.&lt;/p&gt;

&lt;p&gt;Each parallel search now joins the same trace, and the map shows the knowledge bases branching off together.&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem 4: Agents in separate runtimes don't connect
&lt;/h2&gt;

&lt;p&gt;If some of your agents run in &lt;strong&gt;separate&lt;/strong&gt; AgentCore runtimes rather than as tools, a different problem appears. Each agent shows up, but as an island with no line between them.&lt;/p&gt;

&lt;p&gt;The caller has to pass the trace along in the request headers (&lt;code&gt;traceparent&lt;/code&gt; or &lt;code&gt;X-Amzn-Trace-Id&lt;/code&gt;). If it doesn't, the receiving agent starts a new trace.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If you call the other runtime with &lt;code&gt;boto3&lt;/code&gt; (&lt;code&gt;invoke_agent_runtime&lt;/code&gt;), the instrumentation adds the header for you.&lt;/li&gt;
&lt;li&gt;If you call it over plain HTTP, add the header yourself:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;opentelemetry&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;propagate&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;inject&lt;/span&gt;

&lt;span class="n"&gt;headers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"Content-Type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"application/json"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nf"&gt;inject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# adds the current trace to the headers&lt;/span&gt;

&lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;WORKER_URL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;inject()&lt;/code&gt; writes the header formats your &lt;strong&gt;global propagator&lt;/strong&gt; is set up for. ADOT configures this for you. If you set up OpenTelemetry yourself, make sure the propagators match what the receiving side expects, for example &lt;code&gt;OTEL_PROPAGATORS=tracecontext,xray&lt;/code&gt; for both &lt;code&gt;traceparent&lt;/code&gt; and &lt;code&gt;X-Amzn-Trace-Id&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Two small things that cost me time
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Traces take a moment to appear.&lt;/strong&gt; Give it 30 to 60 seconds after a request before you decide it didn't work.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Check the time range and region.&lt;/strong&gt; Open &lt;strong&gt;CloudWatch → X-Ray traces → Service map&lt;/strong&gt;, pick the region your agents run in, and choose &lt;strong&gt;Last 5 minutes&lt;/strong&gt;. An old time range shows an old map.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Check that it worked
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Send a few requests that should take different routes. For example:

&lt;ul&gt;
&lt;li&gt;a refund request, which should go orchestrator → orders agent → refund agent → reply agent&lt;/li&gt;
&lt;li&gt;a policy question, which should go orchestrator → several knowledge bases in parallel → reply agent&lt;/li&gt;
&lt;li&gt;a simple question, which should go straight from the orchestrator to the reply agent&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Wait about a minute.&lt;/li&gt;
&lt;li&gt;Open &lt;strong&gt;CloudWatch → X-Ray traces → Service map&lt;/strong&gt; and choose &lt;strong&gt;Last 5 minutes&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You should see the orchestrator in the middle, a named connection to each worker agent, and each knowledge base search called out separately. Open a single trace too. One request should be one trace, with every worker and knowledge base search inside it.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;[Screenshot: service map before the fixes]&lt;/em&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fg9onqq89cf1awm811ioo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fg9onqq89cf1awm811ioo.png" alt="Screenshot: service map before the fixes" width="800" height="368"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;[Screenshot: service map after the fixes]&lt;/em&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fq3pmxh3rrf75o13mz6ed.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fq3pmxh3rrf75o13mz6ed.png" alt="Screenshot: service map after the fixes" width="800" height="433"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The map also works as a test. If a simple question shows a line to the refund agent, your routing is wrong, not your tracing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Quick checklist
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Transaction Search is on, and you waited about ten minutes.&lt;/li&gt;
&lt;li&gt;OpenTelemetry (ADOT) is in your requirements, and agents outside AgentCore Runtime are started with &lt;code&gt;opentelemetry-instrument&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Sampling is at 100% while you're developing (indexing is a separate setting).&lt;/li&gt;
&lt;li&gt;Each worker-agent call is wrapped in a &lt;code&gt;CLIENT&lt;/code&gt; span with its own &lt;code&gt;peer.service&lt;/code&gt; name.&lt;/li&gt;
&lt;li&gt;Each knowledge base search has its own named span.&lt;/li&gt;
&lt;li&gt;Parallel work copies the context into each thread.&lt;/li&gt;
&lt;li&gt;Calls between separate runtimes pass the trace in their headers.&lt;/li&gt;
&lt;li&gt;You're looking at the right region, with &lt;strong&gt;Last 5 minutes&lt;/strong&gt; selected.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The takeaway
&lt;/h2&gt;

&lt;p&gt;A service map can only draw what your traces describe. Auto-instrumentation records a lot, but it can't know that a function call is really "the refund agent", or that three searches belong together.&lt;/p&gt;

&lt;p&gt;So if your map looks simpler than your system, it doesn't mean your system is broken. It means you need to tell the tracer where the boundaries are: name each agent, name each knowledge base, and carry the trace along wherever your code runs.&lt;/p&gt;

&lt;p&gt;Have you fought with traces in your own multi-agent system? Tell me what tripped you up in the comments.&lt;/p&gt;

&lt;h2&gt;
  
  
  Further reading
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/observability-get-started.html" rel="noopener noreferrer"&gt;Get started with AgentCore Observability&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Enable-TransactionSearch.html" rel="noopener noreferrer"&gt;Enable CloudWatch Transaction Search&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.aws.amazon.com/xray/latest/devguide/xray-services-agentcore.html" rel="noopener noreferrer"&gt;Amazon Bedrock AgentCore and AWS X-Ray&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-migration.html" rel="noopener noreferrer"&gt;Migrating from X-Ray instrumentation to OpenTelemetry&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;I drafted this post with help from an AI assistant, then tested the steps and edited it myself.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>aws</category>
      <category>servicemaps</category>
      <category>multiagent</category>
      <category>ai</category>
    </item>
    <item>
      <title>Debugging agentcore browser deploys from Windows</title>
      <dc:creator>Joy Kareko</dc:creator>
      <pubDate>Fri, 06 Nov 2026 09:39:03 +0000</pubDate>
      <link>https://dev.to/joy_kareko_1cc956e11d827c/debugging-agentcore-browser-deploys-from-windows-4g3a</link>
      <guid>https://dev.to/joy_kareko_1cc956e11d827c/debugging-agentcore-browser-deploys-from-windows-4g3a</guid>
      <description>&lt;p&gt;You build an AI agent on Amazon Bedrock AgentCore and deploy it from your Windows laptop. Everything works: the agent answers questions, calls its tools and remembers things.&lt;/p&gt;

&lt;p&gt;Then you ask it to open a web page, and it crashes with &lt;code&gt;PermissionError: [Errno 13]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The good news? Your code is probably fine. The problem may be a file permission that Windows doesn't preserve when your project is packaged for deployment.&lt;/p&gt;

&lt;p&gt;This is one of those deployment errors that can be frustrating to track down because the agent itself appears to work perfectly. Let's look at what's happening and how to fix it.&lt;/p&gt;

&lt;p&gt;In this post:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What the error looks like&lt;/li&gt;
&lt;li&gt;Why it can happen when deploying from Windows&lt;/li&gt;
&lt;li&gt;Two ways to fix it&lt;/li&gt;
&lt;li&gt;How to check that the fix worked&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What the error looks like
&lt;/h2&gt;

&lt;p&gt;The deployment completes, and &lt;code&gt;agentcore invoke&lt;/code&gt; runs successfully. But any request that needs the browser fails.&lt;/p&gt;

&lt;p&gt;When you open your agent's logs in CloudWatch, you might find an error that ends with a line like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PermissionError: [Errno 13] Permission denied: '/var/task/playwright/driver/node'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The path on your machine may look a little different. What matters is the ending: &lt;code&gt;playwright/driver/node&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If you see that, keep reading.&lt;/p&gt;

&lt;p&gt;The code that leads to this problem is usually nothing unusual. Here's a typical setup for giving an agent a browser tool:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;bedrock_agentcore.runtime&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;BedrockAgentCoreApp&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;strands&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Agent&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;strands_tools.browser&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;AgentCoreBrowser&lt;/span&gt;

&lt;span class="n"&gt;REGION&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;us-east-1&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;BedrockAgentCoreApp&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="n"&gt;browser_tool&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;AgentCoreBrowser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;region&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;REGION&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;agent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Agent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tools&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;browser_tool&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;browser&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Why it happens
&lt;/h2&gt;

&lt;p&gt;First, a quick word about &lt;strong&gt;Playwright&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Playwright is a library that lets your code control a web browser. It can open a page, click buttons, fill in forms and read page content.&lt;/p&gt;

&lt;p&gt;Think of it as a remote control for a browser.&lt;/p&gt;

&lt;p&gt;AgentCore provides access to a browser running in AWS, while Playwright acts as the remote control your agent uses to interact with it.&lt;/p&gt;

&lt;p&gt;Here's the catch.&lt;/p&gt;

&lt;p&gt;The Python version of Playwright doesn't do all the work itself. It comes with a small helper program built on Node.js, which it launches in the background.&lt;/p&gt;

&lt;p&gt;On Linux, a file needs an &lt;strong&gt;execute permission&lt;/strong&gt; to run as a program. Think of this as a simple permission that tells Linux whether a file is allowed to run.&lt;/p&gt;

&lt;p&gt;So, what goes wrong when you deploy from Windows?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;During deployment, the AgentCore CLI packages your project and its dependencies, including Playwright's helper program.&lt;/li&gt;
&lt;li&gt;Windows handles file permissions differently from Linux. When the project is packaged on Windows, the executable permission on Playwright's Node.js helper may not be preserved.&lt;/li&gt;
&lt;li&gt;The package reaches AWS, where your agent runs in a Linux environment.&lt;/li&gt;
&lt;li&gt;The first time your agent tries to use the browser, Playwright attempts to start its helper program.&lt;/li&gt;
&lt;li&gt;Linux sees a file that it isn't allowed to execute and refuses to run it.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That's when you get &lt;code&gt;PermissionError: [Errno 13]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Your other tools may continue working because they're plain Python code. Playwright is different: it needs to launch a separate executable, which is where the permission problem shows up.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fix 1: Deploy from Linux using WSL (the cleanest fix)
&lt;/h2&gt;

&lt;p&gt;If you package your project from Linux, the executable permission can be preserved correctly. You don't need to change your agent code.&lt;/p&gt;

&lt;p&gt;And no, you don't need a second computer.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;WSL (Windows Subsystem for Linux)&lt;/strong&gt; lets you run a Linux environment directly inside Windows. You can use Ubuntu to package and deploy your AgentCore project.&lt;/p&gt;

&lt;p&gt;Here's how.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1: Install WSL
&lt;/h3&gt;

&lt;p&gt;Open PowerShell as Administrator and run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;wsl&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;--install&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Restart your computer if prompted. Ubuntu is installed by default on most supported Windows setups.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2: Copy your project into the Linux filesystem
&lt;/h3&gt;

&lt;p&gt;Open Ubuntu and create a projects folder:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;mkdir&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; ~/projects
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Copy your project from Windows into your Linux home directory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cp&lt;/span&gt; &lt;span class="nt"&gt;-r&lt;/span&gt; /mnt/c/Users/&amp;lt;you&amp;gt;/path/to/my-agent ~/projects/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then navigate into it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd&lt;/span&gt; ~/projects/my-agent
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Important:&lt;/strong&gt; Don't run the deployment directly from &lt;code&gt;/mnt/c/...&lt;/code&gt;. That's still your Windows filesystem, and you may run into the same permission issue.&lt;/p&gt;

&lt;p&gt;Work from your Linux home directory instead.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 3: Set up your deployment environment
&lt;/h3&gt;

&lt;p&gt;Inside WSL, install the tools you need:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;AWS CLI&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;uv&lt;/code&gt; (the &lt;code&gt;direct_code_deploy&lt;/code&gt; option uses it to build your package)&lt;/li&gt;
&lt;li&gt;AgentCore CLI&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Configure your AWS credentials as you normally would.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 4: Deploy your agent
&lt;/h3&gt;

&lt;p&gt;Use your usual AgentCore deployment commands. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;agentcore configure &lt;span class="nt"&gt;--entrypoint&lt;/span&gt; main.py &lt;span class="nt"&gt;--name&lt;/span&gt; my_agent &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--deployment-type&lt;/span&gt; direct_code_deploy &lt;span class="nt"&gt;--runtime&lt;/span&gt; PYTHON_3_13

agentcore deploy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Before deploying, clean up your project folder. Remove unnecessary files such as old deployment archives, build leftovers and virtual environments that shouldn't be included in the package.&lt;/p&gt;

&lt;p&gt;That's it. You're now packaging your project from Linux rather than Windows.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fix 2: Restore the execute permission when the agent starts
&lt;/h2&gt;

&lt;p&gt;What if you can't use WSL?&lt;/p&gt;

&lt;p&gt;There's another approach. You can fix the permission problem when your agent starts, without changing the original file in the deployment package.&lt;/p&gt;

&lt;p&gt;There's one snag: the application code in the AWS runtime is generally read-only, so you can't simply change the permissions on the original file.&lt;/p&gt;

&lt;p&gt;The workaround is to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Copy Playwright's Node.js helper into &lt;code&gt;/tmp&lt;/code&gt;, where your application can write files.&lt;/li&gt;
&lt;li&gt;Give the copied file execute permission.&lt;/li&gt;
&lt;li&gt;Tell Playwright to use the copied executable through the &lt;code&gt;PLAYWRIGHT_NODEJS_PATH&lt;/code&gt; environment variable.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Add the following code near the top of your &lt;code&gt;main.py&lt;/code&gt;, before setting up the browser tool:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;shutil&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;pathlib&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Path&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;playwright&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;fix_playwright_node&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Make Playwright&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;s bundled Node.js executable after a Windows-built deploy.&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="n"&gt;src&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;playwright&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__file__&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;parent&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;driver&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;node&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;src&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;exists&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;access&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;src&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;X_OK&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;  &lt;span class="c1"&gt;# Nothing to fix
&lt;/span&gt;
    &lt;span class="n"&gt;dst&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;/tmp/playwright-node&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;dst&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;exists&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
        &lt;span class="n"&gt;shutil&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copy2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;src&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dst&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;dst&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;chmod&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mo"&gt;0o755&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;PLAYWRIGHT_NODEJS_PATH&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dst&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="nf"&gt;fix_playwright_node&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Why does this work?
&lt;/h3&gt;

&lt;p&gt;Let's break it down.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;It checks before making changes.&lt;/strong&gt; If the original Node.js executable already has execute permission, the function does nothing.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It copies the file to a writable location.&lt;/strong&gt; Instead of trying to modify the original file, it creates a copy in &lt;code&gt;/tmp&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It restores the permission.&lt;/strong&gt; &lt;code&gt;chmod(0o755)&lt;/code&gt; gives the copied file the permissions needed to run.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It tells Playwright where to find the copy.&lt;/strong&gt; The environment variable points Playwright to the new executable.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The timing matters here. The function must run before Playwright starts its helper process. That's why it belongs near the top of your application, before browser initialisation.&lt;/p&gt;

&lt;p&gt;Then redeploy your agent:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;agentcore deploy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Check that it worked
&lt;/h2&gt;

&lt;p&gt;Don't just assume the deployment fixed the problem. Give your agent a task that requires the browser.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;agentcore invoke &lt;span class="s1"&gt;'{"prompt": "Open https://example.com and tell me the page title"}'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If everything works, the agent should reply with the page title, which is "Example Domain".&lt;/p&gt;

&lt;p&gt;You should also check the CloudWatch logs to confirm that the &lt;code&gt;PermissionError&lt;/code&gt; is no longer appearing.&lt;/p&gt;

&lt;p&gt;If the error persists, check the file path mentioned in the new error. It may be a different executable or a separate dependency issue.&lt;/p&gt;

&lt;h2&gt;
  
  
  Quick checklist
&lt;/h2&gt;

&lt;p&gt;Before closing your debugging session, check these things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The error mentions &lt;code&gt;playwright/driver/node&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;You deployed from WSL using your Linux home directory, not &lt;code&gt;/mnt/c/&lt;/code&gt;, or you applied and verified the runtime workaround.&lt;/li&gt;
&lt;li&gt;You cleaned up unnecessary build files before packaging.&lt;/li&gt;
&lt;li&gt;You deployed again using &lt;code&gt;agentcore deploy&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;You tested a browser request.&lt;/li&gt;
&lt;li&gt;The agent returned the page title without a permission error.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The takeaway
&lt;/h2&gt;

&lt;p&gt;When something works on your Windows machine but fails after deployment to Linux, don't immediately start rewriting your agent.&lt;/p&gt;

&lt;p&gt;Sometimes the problem isn't your Python code at all. It may be something as small as a file permission that didn't survive packaging.&lt;/p&gt;

&lt;p&gt;Have you run into a different AgentCore deployment error? Tell me in the comments. I may cover it in a future post.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;I drafted this post with help from an AI assistant, then tested the fixes and edited it myself.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>aws</category>
      <category>python</category>
      <category>playwright</category>
      <category>debugging</category>
    </item>
  </channel>
</rss>
