Speeding Up the Agent › Putting It All Together

Michael BrenndoerferAugust 24, 20252 min read
AI Agent HandbookAll chapters
Part 15 of 15Performance and Cost Optimization - Doing More with Less
Chapter 42Speeding Up the Agent › Putting It All Together
Reading section 12 of 15 · Putting It All Together · 2 minSection 12 of 15 · 2 min

Book contents

Section 12 of 15 · 2 min read

Speeding Up the Agent › Putting It All TogetherOne section · Sections in this chapter12 / 15 ↓

Let's build a fast agent that combines several of these techniques:

In[12]:
Code
import os
from anthropic import Anthropic
import hashlib
import time

class FastAgent:
    """An optimized agent that prioritizes speed."""
    
    def __init__(self):
        self.client = Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))
        self.cache = {}
        self.system_prompt = """You are a helpful assistant. Provide concise, 
        direct answers. Use 1-2 sentences for simple questions."""
    
    def _hash_query(self, query):
        """Create cache key."""
        return hashlib.md5(query.encode()).hexdigest()
    
    def _is_simple_query(self, query):
        """Determine if query is simple enough for fast model."""
        simple_patterns = ["what is", "who is", "when is", "where is"]
        return any(pattern in query.lower() for pattern in simple_patterns)
    
    def respond(self, query):
        """Get fast response using multiple optimization techniques."""
        # 1. Check cache first
        cache_key = self._hash_query(query)
        if cache_key in self.cache:
            return self.cache[cache_key], "cache"
        
        # 2. Choose appropriate model and token limit
        if self._is_simple_query(query):
            max_tokens = 150
            model = "claude-sonnet-4-5"  # Still fast for simple queries
        else:
            max_tokens = 500
            model = "claude-sonnet-4-5"
        
        # 3. Make the call with optimizations
        start = time.time()
        response = self.client.messages.create(
            model=model,
            max_tokens=max_tokens,
            system=self.system_prompt,
            messages=[{"role": "user", "content": query}]
        )
        elapsed = time.time() - start
        
        result = response.content[0].text
        
        # 4. Cache the result
        self.cache[cache_key] = result
        
        return result, f"model ({elapsed:.2f}s)"

## Test the fast agent
agent = FastAgent()

queries = [
    "What is Python?",
    "What is Python?",  # Should hit cache
    "Explain object-oriented programming",
]

for query in queries:
    result, source = agent.respond(query)
    print(f"Q: {query}")
    print(f"A: {result}")
    print(f"Source: {source}\n")
Out[12]:
Console
Q: What is Python?
A: Python is a high-level, interpreted programming language known for its clear syntax and readability. It's widely used for web development, data science, artificial intelligence, automation, and general-purpose programming.
Source: model (3.05s)

Q: What is Python?
A: Python is a high-level, interpreted programming language known for its clear syntax and readability. It's widely used for web development, data science, artificial intelligence, automation, and general-purpose programming.
Source: cache
Q: Explain object-oriented programming
A: **Object-Oriented Programming (OOP)** is a programming paradigm that organizes code around "objects" - data structures containing both data (attributes) and functions (methods) that operate on that data. The four core principles are **encapsulation** (bundling data with methods), **inheritance** (creating new classes from existing ones), **polymorphism** (objects taking multiple forms), and **abstraction** (hiding complex implementation details).
Source: model (3.42s)

This agent combines caching, concise prompts, and smart token limits to deliver fast responses. The first query might take 1.5 seconds, but the cached version is instant. Simple queries use fewer tokens, saving time and money.