Speeding Up the Agent › Precomputing When Possible

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 › Precomputing When Possible
Reading section 10 of 15 · Precomputing When Possible · 1 minSection 10 of 15 · 1 min

Book contents

Section 10 of 15 · 1 min read

Speeding Up the Agent › Precomputing When PossibleOne section · Sections in this chapter10 / 15 ↓

If your agent does the same computation repeatedly, precompute it. For example, if your agent frequently needs to know the current date, time zone conversions, or common calculations, compute these once and reuse them.

In[10]:
Code
from datetime import datetime
import time

class PrecomputedAgent:
    """Agent that caches common computations."""
    
    def __init__(self):
        self.precomputed = {
            'current_date': datetime.now().strftime("%Y-%m-%d"),
            'current_time': datetime.now().strftime("%H:%M:%S"),
            'common_conversions': {
                'miles_to_km': 1.60934,
                'pounds_to_kg': 0.453592,
                'fahrenheit_to_celsius': lambda f: (f - 32) * 5/9
            }
        }
        self.last_update = time.time()
    
    def get_current_date(self):
        """Return precomputed date (refresh if stale)."""
        if time.time() - self.last_update > 3600:  # Refresh every hour
            self.precomputed['current_date'] = datetime.now().strftime("%Y-%m-%d")
            self.last_update = time.time()
        
        return self.precomputed['current_date']
    
    def convert_units(self, value, conversion_type):
        """Use precomputed conversion factors."""
        converter = self.precomputed['common_conversions'].get(conversion_type)
        if callable(converter):
            return converter(value)
        return value * converter

agent = PrecomputedAgent()
print(f"Today's date: {agent.get_current_date()}")  # Instant
print(f"10 miles = {agent.convert_units(10, 'miles_to_km'):.2f} km")  # Instant
Out[10]:
Console
Today's date: 2025-12-07
10 miles = 16.09 km

These operations are instant because the values are precomputed. Compare this to calling a model to do unit conversions or date formatting, which would take seconds.