Hanchett warns agent builders that unchecked loops empty developer wallets

AI Engineer////3 min read

Redesigning Agent Mechanics for Token Efficiency

AI agents in production frequently suffer from bloated operating costs. Developers often default to using frontier models for every sub-task, sending massive context windows back and forth without considering the financial implications. The problem is not the language model itself, but rather the way we structure our agentic workflows. By making deliberate architectural changes, developers can dramatically reduce their token consumption.

Prerequisites

Hanchett warns agent builders that unchecked loops empty developer wallets
Your Agent Is Wasting Tokens and You Don't Know It - Erik Hanchett, AWS

Before implementing these cost-saving techniques, you should have a solid grasp of:

  • Python syntax and asynchronous programming.
  • Core concepts of AI agents, including tool calling and state management.
  • Basics of large language model (LLM) tokenization and context windows.

Key Libraries & Tools

  • Strands: A flexible library designed for building modular AI agents, enabling precise state management and optimization hooks.
  • Claude Sonnet: Anthropic's flagship model used here as the high-tier reasoning engine.
  • Claude Haiku: A highly cost-effective, fast model optimal for lightweight tasks.

Cache System Prompts and Offload Tool Results

One of the easiest ways to save on input tokens is to prevent repetitive data transfer. In Strands, setting cache_prompt = default allows you to store your static system prompt after the first invocation, dramatically lowering the cost of subsequent turns.

# Caching system prompt example
agent = StrandsAgent(
    system_prompt="Your long system instructions go here...",
    cache_prompt="default"
)

Additionally, returning huge raw datasets from tools directly to the agent's context quickly exhausts token limits. Instead, intercept the tool output, save the raw data in external storage, and feed a concise summary back into the main context window.

def summarizer_tool(heavy_data_payload):
    # Save raw payload to database
    db_save(heavy_data_payload)
    # Summarize the payload before returning to the loop
    return summarize_for_agent(heavy_data_payload)

Route by Difficulty and Cap Loops

Not every query requires a premium model like Claude Sonnet. By using a lightweight model to classify the task difficulty first, you can route simpler requests to Claude Haiku.

def route_request(user_input: str):
    is_complex = check_complexity_with_cheap_model(user_input)
    if is_complex:
        return call_model(model_name="claude-3-5-sonnet", prompt=user_input)
    return call_model(model_name="claude-3-haiku", prompt=user_input)

Uncontrolled tools can run endlessly when trying to fix errors. Protect your budget by enforcing hard limits on your iterations:

# Enforcing a loop cap on tools
agent_run = agent.execute(max_iterations=5)

Trim History with Sliding Windows

In multi-turn chat agents, the accumulating conversation history can quickly become a financial liability. Every turn forces the model to re-process the entire past conversation. Use a sliding window to limit the historical context.

from strands import SlidingWindowConversationManager

# Keep only the last 10 messages in active memory
chat_manager = SlidingWindowConversationManager(max_messages=10)

To prevent the agent from losing its bearings entirely, periodically generate a brief summary of the evicted history and inject it as a static anchor inside your active context window.

Topic DensityMention share of the most discussed topics · 8 mentions across 5 distinct topics
Claude Haiku
25%· products
Claude Sonnet
25%· products
Strands
25%· products
AWS
13%· companies
Eric Hanchett
13%· people
End of Article
Source video
Hanchett warns agent builders that unchecked loops empty developer wallets

Your Agent Is Wasting Tokens and You Don't Know It - Erik Hanchett, AWS

Watch

AI Engineer // 5:55

We turn high signal in-person events for the top AI engineers, founders, leaders, and researchers in the world into the best free learning opportunities for millions around the world here on YouTube. Your subscribes, likes, comments, speaking, attendance, or sponsorships goes a long way toward making our biz model sustainable indefinitely. We strongly believe this industry deserves a better class of community and that we know how to do this well; we just need your support.

Who and what they mention most
Anthropic
26.9%21
Claude
21.8%17
OpenAI
19.2%15
Cursor
15.4%12
3 min read0%
3 min read