Agentic optimization strategies

Strategies to reduce tokens, cost, and latency in LLM-powered agentic solutions

Impacts: tokens token usage cost spend latency response time
Prompt optimization
Prompt compression & summarization
Summarize or truncate conversation history instead of passing full transcripts. Use rolling summaries for long contexts.
tokenscost
Lean system prompts
Remove redundant instructions, verbose formatting rules, and unused examples. Every token in the system prompt is paid on every request.
tokenscost
Zero-shot when possible
Avoid injecting many examples if the model can reason zero-shot. Only add examples for tasks with high error rates.
tokenscost
Request structured output
Ask for JSON or a specific schema directly. Avoids verbose prose, easier parsing, fewer retry loops.
tokenslatency
Context window management
RAG over full context injection
Retrieve only the relevant chunks from a knowledge base instead of stuffing the entire document into the context.
tokenscostlatency
Context pruning
Evict old or low-relevance turns from the message history as the conversation grows. Keep only what's needed for the next step.
tokenscost
Prompt caching
Cache static portions of the prompt (system instructions, tool definitions) across requests. Anthropic's cache_control dramatically reduces repeated input costs.
tokenscostlatency
Summarize tool results
Before feeding a tool's output back to the model, condense it. A 200-line API response can become a 5-line summary.
tokenscost
Model selection & routing
Model routing by task complexity
Use small/cheap models (Haiku) for simple classification or extraction tasks. Reserve large models (Opus) for complex reasoning steps only.
costlatency
Speculative / draft models
Use a fast small model to draft tokens, verified by the large model. Significant latency wins for generation-heavy tasks.
latency
Fine-tuned smaller models
For narrow, repetitive tasks, a fine-tuned small model can match large-model quality at a fraction of the cost and latency.
costlatency
Agent architecture patterns
Parallelize independent steps
Run concurrent tool calls and sub-agents in parallel instead of sequentially. Cuts wall-clock time proportionally to the number of independent branches.
latency
Early stopping & short-circuits
Add confidence checks or rule-based filters before invoking the LLM. Skip expensive model calls when a simpler condition can answer the question.
tokenscostlatency
Explicit planning step
Have the agent produce a plan before executing. Avoids redundant tool calls caused by the model "exploring" without direction.
tokenscost
Iteration budgets
Set hard limits on the number of agent loops, tool calls per task, and total context size. Prevents runaway loops from ballooning costs.
tokenscost
Hierarchical decomposition
An orchestrator agent breaks work into subtasks dispatched to specialized sub-agents. Each agent sees only the context it needs.
tokenscostlatency
Tool & function design
Concise tool definitions
Tool schemas and descriptions are injected every request. Keep descriptions tight and parameter names self-explanatory to cut definition overhead.
tokenscost
Tool batching
Design tools that accept lists of inputs rather than single values. One call for 10 records instead of 10 sequential LLM loops.
latencycost
Dynamic tool exposure
Only inject the tools relevant to the current agent phase. A research step doesn't need code execution tools and vice versa.
tokenscost
Infrastructure & delivery
Streaming
Stream tokens to the user as they're generated. Dramatically reduces time-to-first-token perceived latency even if total generation time is unchanged.
latency
Semantic response caching
Cache completed responses to semantically similar queries. A vector similarity check against prior requests avoids re-running the same LLM call.
tokenscostlatency
Batch API for async workloads
Use batch inference endpoints for non-real-time tasks. Anthropic's Batch API offers up to 50% cost reduction at the expense of higher latency.
cost
Output length control
Set tight max_tokens limits and instruct the model to be concise. Output tokens cost the same as input but also drive generation latency linearly.
tokenscostlatency