Building with Claude

Claude is well-suited for agentic applications because of its instruction-following, tool use, and extended thinking capabilities.

Tool Use Basics

Define tools as JSON schemas and Claude will call them when appropriate:

import anthropic

client = anthropic.Anthropic()

tools = [
    {
        "name": "read_file",
        "description": "Read the contents of a file",
        "input_schema": {
            "type": "object",
            "properties": {
                "path": {"type": "string", "description": "File path to read"}
            },
            "required": ["path"]
        }
    }
]

response = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    tools=tools,
    messages=[{"role": "user", "content": "Read the config file at config.json"}]
)

The Agent Loop in Practice

def run_agent(goal: str, tools: list, max_turns: int = 10):
    messages = [{"role": "user", "content": goal}]

    for _ in range(max_turns):
        response = client.messages.create(
            model="claude-sonnet-4-6",
            max_tokens=4096,
            tools=tools,
            messages=messages,
        )

        # Check if Claude wants to use a tool
        if response.stop_reason == "tool_use":
            tool_block = next(b for b in response.content if b.type == "tool_use")
            result = execute_tool(tool_block.name, tool_block.input)

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

    return response  # Max turns reached

Best Practices

  • System prompts — give Claude a clear role, constraints, and output format
  • Structured outputs — use tool definitions to get predictable JSON back
  • Extended thinking — enable for complex reasoning tasks (planning, debugging)
  • Streaming — use stream=True for real-time feedback in interactive applications
  • Temperature — use temperature=0 for deterministic tool use, higher for creative tasks

Cost Management

  • Cache system prompts with prompt caching (up to 90% savings)
  • Use Haiku for simple routing/classification, Sonnet or Opus for complex reasoning
  • Set max_tokens appropriately — don't pay for tokens you won't use