Agent Patterns and Frameworks
Apply the core agent patterns (tool-use loops, subagents, memory, context-window management) to multi-step tasks, and reason about when and how agent frameworks such as Strands, LangGraph and PydanticAI help.
Key points
- 1
Tool-use loop: Claude requests tools (
stop_reason: "tool_use"), your code runs them and returns results, and the loop repeats. Apause_turnstop means a server-tool loop paused; send the assistant content back to continue. - 2
Claude may call several tools in one response. Return all results together, matched by
tool_use_id. If Claude must see each result before choosing the next step, setdisable_parallel_tool_use: trueinsidetool_choice. - 3
Return tool failures as
tool_resultwithis_error: trueand an actionable message. A bare "error" gives the model nothing to change and often causes repeated identical calls. - 4
Subagents start with a fresh conversation, not the parent's history, and only their final response returns to the parent. Pass file paths, errors and decisions in the delegation prompt or in a notes file.
- 5
The model keeps no state between requests, so memory is an application pattern. The memory tool is client-side: Claude requests file operations under
/memories, and your app executes them in storage you control (rejecting paths outside that directory). - 6
Context is a finite attention budget, and quality degrades as it fills with irrelevant tokens ("context rot"). Aim for the smallest set of high-signal tokens; a bigger context window does not cure context pollution.
- 7
Context editing, specifically tool-result clearing, drops old tool results (leaving a placeholder) and keeps the rest verbatim. Compaction summarizes older history and is the broader default strategy for long conversations.
- 8
Just-in-time retrieval: keep lightweight references (paths, queries) and load data with tools when needed. This avoids stale indexes on fast-changing sources. A hybrid approach retrieves some data up front.
- 9
Compaction can drop instructions given early in a conversation. Keep persistent rules in CLAUDE.md, enforce must-hold rules with hooks, and use a
PreCompacthook to archive transcripts if needed. - 10
Long-horizon work across context windows: an initializer session writes a feature list, a progress file and a first commit. Later sessions take one feature at a time, test it end to end, update progress and commit, so each fresh window can rebuild the state.
- 11
LangGraph is a low-level orchestration framework for long-running, stateful agents. It builds graphs of nodes and edges over shared state (
StateGraph) and supports durable execution, human-in-the-loop interrupts and memory. - 12
PydanticAI is a typed Python agent framework: output types and tools are validated with Pydantic models, and dependencies are injected through a typed run context. Strands Agents is an open-source, model-driven agent SDK (Python and TypeScript) with hooks, multi-agent patterns and multiple model providers. All three can use Claude.
- 13
Framework guidance: start with the API directly, since many patterns take only a few lines. Frameworks add abstraction that can obscure prompts and responses, so if quality changes after adopting one, inspect the actual requests it sends.
Read the source
Further reading
Suggest a resource- Cookbook: Memory and context management with Claude(opens in a new tab)Working examples of the memory tool and context editing for agents that run across long sessions.Articleplatform.claude.comEditor’s pick
- The "think" tool for complex tool use(opens in a new tab)When a dedicated think tool helps multi-step tool use, with benchmark results and prompting examples.Articleanthropic.comEditor’s pick
Test yourself on Agent Patterns and Frameworks
Ten questions, with the answer and explanation after each one.