Speeding Up the Agent › Understanding Where Time Goes

Michael BrenndoerferAugust 24, 20251 min read
AI Agent HandbookAll chapters
Part 15 of 15Performance and Cost Optimization - Doing More with Less
Chapter 42Speeding Up the Agent › Understanding Where Time Goes
Reading section 3 of 15 · Understanding Where Time Goes · 1 minSection 3 of 15 · 1 min

Book contents

Section 3 of 15 · 1 min read

Speeding Up the Agent › Understanding Where Time GoesOne section · Sections in this chapter3 / 15 ↓

Before we optimize, we need to understand where the time goes. Let's break down a typical agent interaction:

In[3]:
Code
import time
import os
from anthropic import Anthropic

## Using Claude Sonnet 4.5 for its agent capabilities
client = Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))

def timed_agent_call(user_query):
    """Track timing for each step of agent execution."""
    timings = {}
    
    # Step 1: Send request to model
    start = time.time()
    response = client.messages.create(
        model="claude-sonnet-4-5",
        max_tokens=1024,
        messages=[{"role": "user", "content": user_query}]
    )
    timings['model_call'] = time.time() - start
    
    # Step 2: Process response
    start = time.time()
    result = response.content[0].text
    timings['processing'] = time.time() - start
    
    return result, timings

## Test with a simple query
result, timings = timed_agent_call("What is the capital of France?")
print(f"Model call: {timings['model_call']:.2f}s")
print(f"Processing: {timings['processing']:.2f}s")
print(f"Total: {sum(timings.values()):.2f}s")
Out[3]:
Console
Model call: 2.24s
Processing: 0.00s
Total: 2.24s

When you run this, you'll see something like:

Model call: 1.85s
Processing: 0.01s
Total: 1.86s

The vast majority of time is spent waiting for the model to generate a response. Processing the result is nearly instantaneous. This tells us where to focus our optimization efforts: the model call itself.