The Examples section demonstrates core SDK patterns: building multi-turn conversations by echoing previous messages, handling tool-use responses with matching content blocks and tool_use_id references, streaming events asynchronously, and enabling extended thinking via the thinking parameter. Managed Agents examples show the canonical workflow of creating an environment and agent, starting a session, sending user events, and streaming session events until idle—see the Sessions and Agents resources for the full API.
In examples/messages.py, a multi-turn conversation is built by passing the previous response's role and content directly back into the next messages list, demonstrating the canonical pattern for multi-turn message exchanges.
response2 = client.messages.create(
max_tokens=1024,
messages=[
{"role": "user", "content": "Hello!"},
{"role": response.role, "content": response.content},
{"role": "user", "content": "How are you?"},
],
model="claude-sonnet-5",
)
In examples/tools.py, after a stop_reason == "tool_use" response, the canonical tool-result reply sends the assistant's content block back verbatim and adds a tool_result user turn with tool_use_id referencing the tool block's id.
response = client.messages.create(
model="claude-sonnet-5",
max_tokens=1024,
messages=[
user_message,
{"role": message.role, "content": message.content},
{
"role": "user",
"content": [{"type": "tool_result", "tool_use_id": tool.id, "content": [{"type": "text", "text": "The weather is 73f"}]}],
},
],
tools=tools,
)
Streaming events carry a type discriminator field: "text" events expose a .text attribute and "content_block_stop" events expose a .content_block attribute containing the fully accumulated block.[3] After the async with block exits, stream.get_final_message() can still be called to retrieve the fully accumulated message, provided the entire stream was consumed inside the context manager.[3]
examples/thinking.py demonstrates enabling extended thinking by passing thinking={"type": "enabled", "budget_tokens": 1600} to client.messages.create(); budget_tokens must be less than max_tokens (the example uses budget_tokens=1600 with max_tokens=3200).
response = client.messages.create(
model="claude-sonnet-5",
max_tokens=3200,
thinking={"type": "enabled", "budget_tokens": 1600},
messages=[{"role": "user", "content": "Create a haiku about Anthropic."}],
)
for block in response.content:
if block.type == "thinking":
print(f"Thinking: {block.thinking}")
elif block.type == "text":
print(f"Text: {block.text}")
When extended thinking is enabled in examples/thinking.py, the response's content list contains both "thinking" blocks (exposing block.thinking) and "text" blocks (exposing block.text); iterating code must check block.type to distinguish them.
The examples/agents.py script demonstrates the canonical end-to-end Managed Agents workflow: create an environment, create an agent, create a session referencing both, send a user.message event, then stream session events until session.status_idle is received — see Sessions resource and Agents resource for the full API surface.
environment = anthropic.beta.environments.create(name="simple-example-environment")
agent = anthropic.beta.agents.create(name="simple-example-agent", model="claude-sonnet-5")
session = anthropic.beta.sessions.create(
environment_id=environment.id,
agent={"type": "agent", "id": agent.id, "version": agent.version},
)
anthropologic.beta.sessions.events.send(
session.id,
events=[{"type": "user.message", "content": [{"type": "text", "text": "Hello Claude!"}]}],
)
with anthropic.beta.sessions.events.stream(session.id) as stream:
for event in stream:
if event.type == "session.status_idle":
break
Sources