Anthropic SDK
You run the agent loop with the Anthropic SDK; one call turns Claude's tool calls into the tool results to send back, each one shrunk the moment the tool returns.
Setup#
Add the expand tool to your tools, and replace the code that runs tools and builds tool_result blocks with one call:
Your tools stay as they are. The call runs them through a function you give it:
What the call does#
For every tool_use block in the response, in order:
- Runs your tool with the arguments Claude chose, and keeps the raw output on your side.
- Shrinks the output for the model: noise removed, the relevant parts kept within the tool's budget. Small results, and results where a cut would save too little (under 1,000 tokens or 30%), pass whole.
- Answers
expanditself, from the original output, so trimmed parts are one call away. It never reaches your tools. - Reports failures: a tool that throws becomes a
tool_resultwithis_errorand the message, so Claude can react instead of your loop crashing.
It returns the tool_result blocks, in the order of the calls, ready for the next user message. The history keeps the shrunk view, so later turns resend the same bytes and the prompt cache keeps hitting.
The request#
Relevance is judged against what the agent is after. The call combines the text Claude wrote before the tool calls with request: pass the user's request for the turn. Without it, only the tool's arguments and Claude's text are used.
Parallel tools#
When Claude asks for several tools in one response, they run in parallel. runTool can be sync or async.
Serverless and scripts#
Dashboard reports are sent in the background. In a short-lived process, await cw.flush() before it ends.