DEV Community

Thulani Zondo
Thulani Zondo

Posted on • Originally published at learnhowtobuildaiagents.com

Build Your First AI Agent with Claude (Anthropic) — Python Tutorial

Claude is Anthropic's AI model with a 200K token context window and native tool use. Here's how to build an agent with it.

Setup

pip install anthropic
Enter fullscreen mode Exit fullscreen mode

Basic Agent

import anthropic
import json

client = anthropic.Anthropic(api_key="your-key")

tools = [{
    "name": "calculate",
    "description": "Perform mathematical calculations",
    "input_schema": {
        "type": "object",
        "properties": {
            "expression": {"type": "string"}
        },
        "required": ["expression"]
    }
}]

def agent_with_tools(task: str) -> str:
    messages = [{"role": "user", "content": task}]

    while True:
        response = client.messages.create(
            model="claude-sonnet-4-20250514",
            max_tokens=2048,
            tools=tools,
            messages=messages
        )

        if response.stop_reason == "tool_use":
            tool_block = next(b for b in response.content if b.type == "tool_use")
            result = {"result": eval(tool_block.input["expression"])}

            messages.append({"role": "assistant", "content": response.content})
            messages.append({"role": "user", "content": [{
                "type": "tool_result",
                "tool_use_id": tool_block.id,
                "content": json.dumps(result)
            }]})
        else:
            return response.content[0].text

print(agent_with_tools("What is 1547 * 382 + 99?"))
Enter fullscreen mode Exit fullscreen mode

Why Claude for Agents?

  • 200K token context window
  • Native tool use (no workarounds)
  • Extended thinking for complex reasoning
  • Constitutional AI for safety

Learn More

Full course covering Claude, LangChain, LlamaIndex, CrewAI, and MCP:

👉 learnhowtobuildaiagents.com — Module 1 free, no signup needed

Top comments (0)