Skip to content

Commit 892f846

Browse files
committed
fix(managed_agents): align memory cookbook with CMA series conventions
- Guard the session.status_idle break on stop_reason.type == "end_turn" so the pattern is safe to copy into custom-tool agents - Import wait_for_idle_status from utilities and call it before each sessions.archive() to absorb the SSE-vs-status race - Use environments.archive instead of environments.delete - Define MODEL via the COOKBOOK_MODEL env var like the other CMA notebooks and pass it to agents.create - Seed cell: capture the return, print the seeded path, and note that it must run before the cleanup cell that deletes the store https://claude.ai/code/session_01VkfVPdiJTWVh6Vj5owqRXH
1 parent 1016f8f commit 892f846

1 file changed

Lines changed: 15 additions & 5 deletions

File tree

managed_agents/CMA_remember_user_preferences.ipynb

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,12 @@
5959
"execution_count": 1,
6060
"outputs": [],
6161
"source": [
62+
"import os\n",
63+
"\n",
6264
"from anthropic import Anthropic\n",
65+
"from utilities import wait_for_idle_status\n",
66+
"\n",
67+
"MODEL = os.environ.get(\"COOKBOOK_MODEL\", \"claude-sonnet-4-6\")\n",
6368
"\n",
6469
"client = Anthropic()"
6570
]
@@ -111,7 +116,8 @@
111116
" print(f\" [memory] {event.name}: {target}\")\n",
112117
"\n",
113118
" elif event.type == \"session.status_idle\":\n",
114-
" break\n",
119+
" if event.stop_reason and event.stop_reason.type == \"end_turn\":\n",
120+
" break\n",
115121
"\n",
116122
" return \"\".join(reply_parts)"
117123
]
@@ -187,7 +193,7 @@
187193
"\n",
188194
"agent = client.beta.agents.create(\n",
189195
" name=\"Personal Shopper\",\n",
190-
" model=\"claude-sonnet-4-6\",\n",
196+
" model=MODEL,\n",
191197
" system=(\n",
192198
" \"You are a personal shopping assistant for a retail brand. \"\n",
193199
" \"Help the customer find products that match their taste and budget, \"\n",
@@ -474,15 +480,17 @@
474480
"execution_count": 9,
475481
"outputs": [],
476482
"source": [
477-
"client.beta.memory_stores.memories.create(\n",
483+
"# Run before the cleanup cell below; `store` is deleted there.\n",
484+
"seeded = client.beta.memory_stores.memories.create(\n",
478485
" store.id,\n",
479486
" path=\"/purchase-history.md\",\n",
480487
" content=(\n",
481488
" \"## Recent purchases\\n\"\n",
482489
" \"- Canvas tote, olive, $89 (Jan 2026)\\n\"\n",
483490
" \"- Wool beanie, rust, $34 (Dec 2025)\\n\"\n",
484491
" ),\n",
485-
")"
492+
")\n",
493+
"print(f\"Seeded {seeded.path}\")"
486494
]
487495
},
488496
{
@@ -542,11 +550,13 @@
542550
"execution_count": 10,
543551
"outputs": [],
544552
"source": [
553+
"wait_for_idle_status(client, session_one.id)\n",
545554
"client.beta.sessions.archive(session_one.id)\n",
555+
"wait_for_idle_status(client, session_two.id)\n",
546556
"client.beta.sessions.archive(session_two.id)\n",
547557
"client.beta.memory_stores.delete(store.id)\n",
548558
"client.beta.agents.archive(agent.id)\n",
549-
"client.beta.environments.delete(environment.id)"
559+
"client.beta.environments.archive(environment.id)"
550560
]
551561
},
552562
{

0 commit comments

Comments
 (0)