PythonClaude APITool UsePrompt Caching

Claude Assist

An AI coding agent powered by Anthropic's Claude API. Give it a natural-language task and it autonomously reads files, writes code, and runs Python scripts — iterating until the task is complete or a step limit is reached. Built as a port of PCA (the Gemini-based agent) to study the differences between Google and Anthropic's function-calling APIs.

Features

  • Tool use: Claude decides which tools to call based on the task
  • File operations: read files, list directories, write or overwrite code
  • Code execution: run Python scripts and feed stdout/stderr back to the model
  • Multi-turn loop: continues until stop_reason == "end_turn" or 20 iterations
  • Prompt caching: system prompt marked with cache_control to reduce repeated token costs
  • Verbose mode: log each tool call name, input, and truncated result

How It Works

The agent loop sends the user prompt to Claude with a set of tool definitions. When Claude responds with stop_reason == "tool_use", the agent extracts each tool_use block, calls the corresponding Python function, and appends a tool_result message. This continues until Claude returns a final text response.

while True:
    response = client.messages.create(
        model="claude-sonnet-4-6",
        max_tokens=8096,
        system=system,  # cache_control: ephemeral on system prompt
        tools=TOOLS,
        messages=messages,
    )
    if response.stop_reason == "end_turn":
        break
    # extract tool_use blocks, call functions, append tool_result

Usage

python3 src/main.py "list files in the working directory"
python3 src/main.py "fix the bug in fix_bug.py" --verbose
python3 src/main.py "write unit tests for main.py" --dir ./my_project

Comparison with PCA (Gemini)

The core agent loop is structurally identical — both iterate on tool calls until a final response. The main differences are in the API shape:

  • Tool schemas use Anthropic's JSON Schema format instead of google.genai.types.FunctionDeclaration
  • stop_reason == "tool_use" replaces checking for function_call parts in the Gemini response
  • Prompt caching is explicit via cache_control on the system message
  • The Anthropic SDK is strictly typed — response.content is a list of typed blocks (TextBlock, ToolUseBlock)

Tech Stack

  • Language: Python 3.10+
  • AI API: Anthropic Claude (anthropic SDK)
  • Model: claude-sonnet-4-6
  • Config: python-dotenv for API key management
  • Architecture: Tool-use agent loop (up to 20 iterations)