From the 80-line hand-rolled loop to the SDK that takes most of it off your plate.
Agents Module 2 Claude Agent SDK
for turn in range(MAX_TURNS):).tool_result block).MAX_TURNS ceiling).is_error: True wrapping on except).The Claude Agent SDK gives you all of those as one Agent(...).run(...) call.
There is no separate claude-agent-sdk PyPI package in 2026 Q2. The SDK is a set of higher-level primitives — Agent, tool, Tool, message helpers — exposed inside the official anthropic Python package.
You install one thing:
pip install anthropic
…and import both the low-level Anthropic() client and the SDK primitives from the same package.
anthropic namespace.If your search engine sends you to old Computer Use docs, you're reading the right SDK under its old name.
AgentWraps the loop. You give it a model, a system prompt, a tool list, and call .run(user_message).
@tool / ToolDecorator + class to declare a Python function as an LLM-callable tool. Pulls types and docstring into the JSON Schema for you.
assistant_message, tool_result, … — so you don't hand-write the {"role": ..., "content": [...]} shape.
Built-in max_turns, max_cost_usd, and an on_step hook for caller-driven interrupts.
Plus: .stream() and per-event callbacks for token-by-token output and observability.
from anthropic import Anthropic
from anthropic.agents import Agent, tool
@tool
def get_weather(city: str) -> str:
"""Get current weather for a city."""
return f"{city}: 4°C, light rain"
agent = Agent(
client=Anthropic(),
model="claude-3-5-sonnet-latest",
system="You are a helpful weather assistant.",
tools=[get_weather],
)
result = agent.run("What's the weather in Kyiv?")
print(result.final_text)
This is the same agent as L02's 80-line version, in 12 lines. The SDK provides exactly what we wrote by hand last lesson.
| You wrote by hand in L02 | SDK does for you |
|---|---|
The for turn in range(MAX_TURNS): loop | Agent.run() |
Branch on stop_reason | internal |
Append assistant + tool_result blocks | internal |
| Cost tracking | result.usage + max_cost_usd= arg |
Tool-error → is_error: True | internal try / except in @tool wrapper |
| Turn cap | max_turns= arg |
@tool decorator in detailinput_schema automatically.description — the LLM uses this to decide whether to call it.is_error: True tool result automatically.TOOLS list with hand-written JSON Schema is gone.agent = Agent(
client=Anthropic(),
model="claude-3-5-sonnet-latest",
system="...",
tools=[...],
max_turns=10, # turn cap
max_cost_usd=0.50, # cost cap
on_step=log_step, # caller hook, can raise to interrupt
)
Three of the four L01 stop conditions are now config. Natural exit (end_turn) is automatic. Caller interrupt is on_step raising.
agent.stream(...) yields events as the agent runs:message_start — new turn begins.tool_use — LLM is calling a tool.text_delta — token streaming for the final answer.tool_result — tool returned (or errored).end_turn — agent finished.Render token-by-token, log each step, or pipe events to an observability backend. The free-tier event surface is much richer than what hand-rolled L02 gave you.
This is the sweet spot — and exactly what L04 builds.
Agent(...) construction, stop config, streaming — gets rewritten.anthropic. One pip install, no separate package.Agent(...).run(...).