Skip to main content

Slow Tool Execution

Symptom

Tools take too long to execute

Solutions

1. Use Async for I/O

# ❌ Slow - blocks event loop
@tool("Send email")
def send_email(self, email: str):
    return email_service.send(email)  # Blocking

# ✅ Fast - non-blocking
@tool_async("Send email")
async def send_email(self, email: str):
    return await email_service.send_async(email)

2. Add Caching

from functools import lru_cache

@lru_cache(maxsize=100)
@tool("Get product")
def get_product(self, product_id: str):
    """Cached product lookup"""
    return database.get(product_id)

3. Optimize Database Queries

# ❌ Slow - N+1 queries
def get_orders(self, customer_id: str):
    orders = db.query("SELECT * FROM orders WHERE customer_id = ?", customer_id)
    for order in orders:
        order['items'] = db.query("SELECT * FROM items WHERE order_id = ?", order['id'])

# ✅ Fast - single query with join
def get_orders(self, customer_id: str):
    return db.query("""
        SELECT o.*, i.*
        FROM orders o
        LEFT JOIN items i ON i.order_id = o.id
        WHERE o.customer_id = ?
    """, customer_id)

High Memory Usage

Solutions

Clean Up State

def on_conversation_ended(self, conversation_id: str):
    """Always clean up"""
    self.conversations.pop(conversation_id, None)
    self.cache.clear(conversation_id)

Slow Response Times

Solutions

Monitor Performance

import time

@tool("My tool")
def my_tool(self, param: str):
    """Monitor execution time"""
    start = time.time()

    result = process(param)

    duration = time.time() - start
    if duration > 2.0:
        logger.warning(f"Slow tool: {duration:.2f}s")

    return result

Quick Fixes

IssueFix
Slow toolsUse async/await
High memoryClean up state
Slow DBOptimize queries
Repeated callsAdd caching

Scaling

Scale your agent