
Saving Money with AI Agents
Building with AI agents moves fast, but the bills move faster. Whether you are running a single autonomous assistant or orchestrating a multi-agent graph, cost discipline separates prototypes that survive from ones that get shut down. These twenty tips draw on hard-won lessons from the field — from intelligent routing inspired by Spotify's Shunt approach, to the unglamorous work of trimming token waste from tool schemas.
Not every task needs GPT-4 or Claude Opus. Build a routing layer that dispatches simple queries to cheaper models (Haiku, Gemini Flash) and reserves expensive models for reasoning-heavy work. Spotify's Shunt philosophy — intelligent traffic shunting — applies directly here.
Exact-match caching only saves you when prompts are identical. Semantic caching finds near-duplicate queries and returns stored answers, cutting a large share of redundant LLM calls.
Anthropic, OpenAI, and Google all offer prompt prefix caching. Pin your system prompt and static context as a cached prefix — re-reads cost a fraction of full input tokens.
Track tokens, latency, and cost per agent step from day one. You cannot cut what you cannot see. Attach an observability layer before doing anything else.
Add a token or dollar ceiling to every agent run. When the budget is hit, surface a graceful fallback rather than silently burning credits on a runaway loop.
Summarisation, classification, and formatting tasks often need no proprietary API. Run Llama, Mistral, or Qwen locally for free on commodity hardware.
Most cloud providers discount async batch endpoints by 50 % or more. Queue low-priority agent work (nightly reports, bulk enrichment) and flush it through the batch API.
Retrieval-Augmented Generation lets you fetch only the relevant chunks at query time rather than cramming an entire knowledge base into every prompt. Fewer tokens, same quality.
Ill-defined prompts that return free-text often require a follow-up call to extract structured data. Force JSON or typed schemas on the first call and cut those retries entirely.
If an agent sub-task is looping or hitting errors repeatedly, a circuit breaker stops the bleeding. Without one, a single misconfigured tool can exhaust a daily budget in minutes.
As an agent conversation grows, periodically summarise completed steps into a compact note rather than carrying the full transcript. Shorter context = lower cost per turn.
If your agent repeatedly calls a large model to do the same narrow task (intent classification, slot filling), a fine-tuned 7 B model can match the quality at a fraction of the price.
Tool schemas are injected into every call. Verbose descriptions with unnecessary examples can add hundreds of tokens per turn. Audit and trim tool definitions regularly.
Different providers price identically-capable models very differently. A load balancer that routes to whichever provider is cheapest at the moment can cut costs without touching your code.
Unbounded parallel agent runs spike costs unpredictably. Cap concurrency with a semaphore or queue, and shape traffic so you stay within provider rate-limit tiers.
High temperature = more tokens wasted on exploration. Start deterministic runs (temp 0) and only raise temperature when you actually need creative variance.
For agents that run multi-step workflows, serialise state between steps. On failure you resume from a checkpoint rather than replaying the entire (expensive) run from scratch.
Stream cost events into a metering system so you can alert on anomalies before end-of-month surprises. Treat token spend like any other infrastructure metric.
Embedding the same documents on every run is pure waste. Store vectors in a persistent index and only re-embed when the source changes.
Regular log reviews surface agent paths that always fail or never contribute to the final answer — dead weight that costs tokens on every run. Kill them.
Perhaps it would be more estetic to have it like this: Made with ❤️ by dirkjbosman © (sitemap)
The common thread across all twenty is the same: measure first, then cut. Blind optimisation wastes engineering time. A solid observability layer, a routing proxy, and a caching strategy will cover 80 % of most cost problems — the remaining 20 % is usually a misbehaving loop or an oversized context that a quick audit will surface.
New articles, tools and experiments — no noise.