Claude API

Advanced API features for building agentic applications: context compaction, tool use enhancements, computer use, and web capabilities.

Context Compaction

Long agentic sessions accumulate context rapidly. Compaction automatically summarizes earlier turns to stay within limits.

Configuration

{
  "compaction": {
    "strategy": "compact-2026-01-12",
    "threshold": 0.8,
    "pause_after_compaction": false
  }
}
ParameterDefaultDescription
strategycompact-2026-01-12Compaction algorithm version
threshold0.8Trigger at 80% of context window
pause_after_compactionfalsePause for user review after compacting

What Gets Preserved

  • Recent tool outputs and their conclusions
  • Code changes and rationale
  • User instructions and corrections
  • Active task context

What Gets Compressed

  • Intermediate search results
  • Verbose tool outputs that led to conclusions already captured
  • Redundant exploration steps
  • Superseded file contents (older versions of edited files)

Advanced Tool Use

Three features that improve tool use at scale:

Tool Search Tool

When agents have access to many tools (50+), the model can waste tokens evaluating irrelevant ones. Tool Search Tool auto-selects relevant tools from large sets:

  • 85% token reduction in tool-heavy scenarios
  • Model sees a lightweight search tool instead of all tool definitions
  • Searches and loads relevant tool schemas on demand

Programmatic Tool Calling

Claude writes Python code to orchestrate multiple tool calls instead of making them one at a time:

# Instead of 10 sequential tool calls, Claude generates:
results = []
for file in ["auth.py", "routes.py", "models.py"]:
    content = read_file(file)
    results.append(analyze(content))
summary = summarize(results)
  • 37% token savings compared to sequential calls
  • Reduces round trips between model and tool executor

Tool Use Examples

Add input_examples to tool definitions to improve accuracy:

{
  "name": "search_logs",
  "description": "Search application logs",
  "input_schema": {
    "type": "object",
    "properties": {
      "query": { "type": "string" },
      "timeframe": { "type": "string" }
    }
  },
  "input_examples": [
    { "query": "error 500", "timeframe": "1h" },
    { "query": "user login failed", "timeframe": "24h" }
  ]
}
  • Improves tool call accuracy from 72% to 90% in benchmarks
  • Especially useful for tools with non-obvious parameter formats

Deferred Tool Loading

Load tool schemas lazily to reduce initial prompt size:

{
  "tools": [
    {
      "name": "complex_analysis",
      "defer_loading": true
    }
  ]
}

The model sees the tool name but not its full schema. When it decides to use the tool, the schema is fetched and loaded.

Allowed Callers

Restrict which agents can use specific tools:

{
  "name": "deploy_production",
  "allowed_callers": ["lead-agent"],
  "input_schema": { ... }
}

Web Search and Fetch

Both are now GA across all models:

response = client.messages.create(
    model="claude-sonnet-4-6",
    tools=[
        {"type": "web_search"},
        {"type": "web_fetch"},
    ],
    messages=[{"role": "user", "content": "What's the latest release of React?"}],
)
  • Dynamic filtering via code execution (model writes JS/Python to parse results)
  • Code execution is free when used alongside web search/fetch

Fine-Grained Tool Streaming

Stream individual tool use events as they happen:

with client.messages.stream(
    model="claude-sonnet-4-6",
    tools=tools,
    messages=messages,
) as stream:
    for event in stream:
        if event.type == "tool_use_start":
            print(f"Starting tool: {event.tool.name}")
        elif event.type == "tool_use_input_delta":
            print(f"Input chunk: {event.delta}")
        elif event.type == "tool_use_end":
            print(f"Tool complete")

GA across all models. Enables real-time UI updates showing which tool is being called and with what parameters.

Computer Use

GUI automation through screenshots and input simulation:

response = client.messages.create(
    model="claude-sonnet-4-6",
    tools=[{"type": "computer_20250124", "display_width": 1920, "display_height": 1080}],
    messages=[{"role": "user", "content": "Open the settings page and change the theme to dark"}],
)

Available Actions

ActionDescription
screenshotCapture current screen state
clickClick at coordinates
typeType text
keyPress key combination
hold_keyHold a key while performing other actions
left_mouse_downPress and hold left mouse button
scrollScroll in a direction
triple_clickSelect an entire line/paragraph
waitWait for a specified duration

Next