How to Intermediate · 3 min read

How to build a multi-step agent with Anthropic API

Quick answer
Use the anthropic.Anthropic client to create a multi-turn conversation by managing the messages array and calling client.messages.create() repeatedly with updated context. Chain multiple steps by appending user and assistant messages to simulate an agent workflow.

PREREQUISITES

  • Python 3.8+
  • Anthropic API key
  • pip install anthropic>=0.20

Setup

Install the Anthropic Python SDK and set your API key as an environment variable.

bash
pip install anthropic>=0.20

Step by step

This example demonstrates a multi-step agent that first summarizes text, then answers a question about it, using the claude-3-5-sonnet-20241022 model.

python
import os
import anthropic

client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])

system_prompt = "You are a helpful assistant."

# Step 1: Summarize the input text
messages = [
    {"role": "user", "content": "Summarize the following text:\n\nAnthropic is an AI safety and research company."}
]
response1 = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=200,
    system=system_prompt,
    messages=messages
)
summary = response1.content[0].text
print("Summary:", summary)

# Step 2: Ask a follow-up question using the summary
messages.append({"role": "assistant", "content": summary})
messages.append({"role": "user", "content": "What is Anthropic's main focus?"})

response2 = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=200,
    system=system_prompt,
    messages=messages
)
answer = response2.content[0].text
print("Answer:", answer)
output
Summary: Anthropic is a company focused on AI safety and research.
Answer: Anthropic's main focus is AI safety and research.

Common variations

  • Use async calls with client.messages.acreate() for concurrency.
  • Switch models, e.g., claude-3-opus-20240229 for faster responses.
  • Implement streaming by setting stream=True and handling partial outputs.
  • Manage longer conversations by truncating or summarizing earlier messages.

Troubleshooting

  • If you get rate limit errors, add retries with exponential backoff.
  • For context length errors, reduce max_tokens or shorten conversation history.
  • If responses are off-topic, refine the system prompt for clearer instructions.

Key Takeaways

  • Build multi-step agents by managing the messages list to maintain conversation context.
  • Use the claude-3-5-sonnet-20241022 model for best coding and reasoning performance.
  • Append assistant and user messages between calls to simulate multi-turn workflows.
  • Handle errors by managing token limits and refining system prompts.
  • Async and streaming modes enable more efficient and interactive agents.
Verified 2026-04 · claude-3-5-sonnet-20241022
Verify ↗