Claude and the Anthropic SDK · Chapter 19 of 21
An agent the Claude way
By the end of this chapter you have built Sahay as an agent: a loop that asks Claude, runs the tools it asks for, sends the results back and stops on its own, with the policy cached for every family, the growing conversation cached turn by turn, thinking blocks passed back untouched, a branch for every stop reason, and a limit on steps and on dollars. You can print where every dollar of a run went. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Ship it
- 4 tasks
- Python
- In the all labs plan
The first 2 chapters of Claude and the Anthropic SDK are free. This one comes with the all labs plan.
Why you need this
Every piece of Sahay works on its own by now. Chapter 6 wrote a tool loop, chapter 8 let the SDK run one, chapters 13 and 14 cached the policy, chapters 15 and 16 showed what thinking costs and why its blocks must go back unchanged. Meher wants one Sahay that does all of it, every day, for every family, and Farhan wants to know what each answer costs before he signs off on four hundred a week.
Putting the pieces together is where they break. A loop with no cache on the conversation pays for the whole history again every turn. A loop that tidies its history (drops the empty thinking blocks, fixes a typo in an old message) gets a 400 or loses its cache. A loop that treats "not tool_use" as "done" tells a family "I'll look up CLM-2402 first." and nothing more. A loop with no limits is a bill with no ceiling. This chapter builds the loop that has none of those bugs.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
turn.py: one turn of Sahay
Define SYSTEM: one text block of PROMPT (given) with "cache_control": {"type": "ephemeral"}. Define turn(messages, model="claude-opus-5-5"): one request with max_tokens 16000, system=SYSTEM, tools=TOOLS, the messages and a top-level cache_control={"type": "ephemeral"}. Define tool_results(reply): one tool_result per tool_use block, in order, with run_tool's .text and .is_error.
Under if __name__ == "__main__":, send one turn of How much will CLM-2410 pay? and print stop_reason: STOP, blocks: TYPES, results: N (E errors) and cache write: W, cache read: R.
- Task 2
sahay.py: the loop, with its limits
Define dollars(usage, model) (input, cache writes at cache_write_5m, cache reads and output at PRICES[model], per million) and sahay(question, model="claude-opus-5-5", max_steps=8, max_dollars=0.25) returning Run(answer, stop, steps, dollars) (Step and Run are given).
Loop with turn(): record each Step (stop_reason, tool names, usage, dollars), append the reply exactly as it came; on any stop_reason but tool_use return its text and that stop; at max_steps steps return stop "max_steps", once the dollars reach max_dollars stop "max_dollars" (answer "" for both); else append one user message of tool_results(reply). The check runs claims it never showed you, on Opus and Sonnet.
- Task 3
transcript.py: a transcript, step by step
Run sahay("How much will CLM-2410 pay?") and print one line per step: step N: STOP TOOLS | in I, write W, read R, out O | $D (TOOLS joined with ", ", left out when there are none; D with 6 decimals). Then answer: and the first line of the answer, and total: N steps, $D.
- Task 4 · interview question
Interview: cut_short.py
"Sahay told a family: I'll look up CLM-2407 first. Then nothing. Why?" Define what_next(reply): "run_tools" for tool_use, "cut_off" for max_tokens, "refused" for refusal, "resume" for pause_turn, "answer" for end_turn and stop_sequence. Define safe_sahay(question, max_tokens): naive()'s loop (given) with what_next(): a cut reply is not kept, and the same messages go again with max_tokens doubled (at most 16000, kept for later turns); refused returns REFUSED; resume appends the reply and sends again; at most 12 requests.
Under if __name__ == "__main__":, print naive: and what naive("How much will CLM-2407 pay?", 60) said, then safe: and the first line of safe_sahay with the same question and 60.
What you will learn
- What an agent is here: a loop, a rule for when it stops, and limits.
- One turn's request: the cached system block, the tools, and top-level automatic caching for the conversation.
- Passing a reply back exactly as it came, thinking blocks and all, and what stripping or editing them does.
- Every stop reason and what the loop does with each one.
- Budgets: a step limit and a dollar limit, priced from usage.
- A transcript: tokens and dollars per step.
More in Ship it
Build "An agent the Claude way" yourself
Parchi, a Jaipur startup, helps families claim health insurance. You build Sahay, a claims assistant that reads bills and policies with Claude, and measure what caching, models and effort cost. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Claude and the Anthropic SDK are free. This one comes with the all labs plan.