executing-plans
executing-plans
Use when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is available
llm-api-optimizationUse when an LLM-backed application's API calls need to get cheaper, faster or more reliable — prompt caching and the prefix rules that decide whether it hits, batch processing, context engineering and what earns a place in the window, payload format economics, structured outputs and constrained decoding, model routing by task, streaming and latency, and mea…
Independent GitHub snapshot · Automated classification · Original documentation governs
Verified 2026-07-29. Prices, multipliers and TTLs move constantly and differ per provider — every number below is a shape to reason with, and must be re-checked against current provider documentation before it goes in a budget.
| Lever | Typical saving | Effort |
|---|---|---|
| Prompt caching | Up to ~90% on the repeated portion | Low — often a few lines |
| Batch processing | ~50% on both input and output | Low, if latency permits |
| Context engineering | 40–60% | Medium — ongoing discipline |
| Payload format | 20–50% on structured payloads | Low |
| Model routing | Large, task-dependent | Medium |
Caching and batching stack multiplicatively — combined they can take the repeated portion of a workload to a small fraction of list price. Realistic end-to-end reduction on a well-engineered application is around 70–80%.
Do the cheap structural work before switching to a smaller model. Model downgrades trade quality for cost; caching does not.
The cache keys on an exact prefix of the request. Everything before the change is reusable; everything from the first differing byte onward is not.
Therefore: static content first, dynamic content last. System prompt, tool definitions and documents at the top; the user's turn at the bottom. This one ordering rule decides whether caching works at all.
Typical economics (verify against current docs):
| Multiplier | |
|---|---|
| Cache write, short TTL | ~1.25× base input |
| Cache write, extended TTL | ~2× base input |
| Cache read | ~0.1× base input |
Break-even is one hit on a short TTL, roughly two on an extended one. So the calculation is not "is caching good" but "will this prefix be read again inside its TTL". A prefix read once costs you money.
Extended TTL wins where the natural gap between requests exceeds the short window — a support agent handling a case over half an hour, an interactive session with thinking time. Paying the write premium once beats re-writing every few minutes.
Do not cache what will not be re-read. A one-shot call with a large unique document is the canonical waste.
Every one of these silently drops the hit rate to zero while the code looks correct:
Instrument the hit rate. Providers report cache-read and cache-write tokens per response. A caching implementation nobody measured is usually a caching implementation that is not working.
Asynchronous batch submission typically returns within a day at around half price on input and output, and stacks with caching.
Fits: evaluation runs, back-fills, bulk classification, summarising a document corpus, nightly enrichment. Does not fit: anything a user is waiting for.
Look for the interactive work that is not actually interactive. Enrichment triggered by a user action but consumed hours later is batch work wearing a synchronous costume.
Prompt engineering is what you say; context engineering is what you provide. On production systems the second dominates the bill and most of the quality.
rag-architecture §5.JSON costs roughly twice the tokens of YAML or TSV for the same data — quotes, braces and repeated keys on every record.
Measure before rewriting a serialiser. This lever is large on high-volume structured payloads and negligible on prose-shaped workloads.
Two approaches, and the difference matters under load:
| Approach | How | Trade-off |
|---|---|---|
| Constrained decoding | The decoder can only emit tokens valid for the schema | Structurally guaranteed; needs provider or local support |
| Schema-in-prompt + validate + retry | Ask for a shape, validate, retry on failure | Portable; retries cost tokens and latency |
Validate the output regardless of which you use. Schema-valid is not the same as semantically correct — a required field can be present and wrong. Types are not a business-rule check.
Never parse structure out of prose. Regexing an amount out of a sentence works in testing and fails on the first unusual phrasing.
Route by task, not by habit. Classification, extraction, routing and formatting rarely need the largest model; multi-step reasoning, ambiguous judgement and code architecture do.
Build the routing decision as a policy in one place, not as a hard-coded model id at each call site —
that is what makes re-tiering a config change instead of a refactor. Inside this harness, that policy
is smart-config; in an application, a small resolver reading configuration does the same job.
Escalate on evidence. A cheap model plus a validity check, escalating on failure, is often cheaper and better than the large model everywhere — provided the check is real.
Per-call cost is the wrong denominator. A cheaper model that needs three attempts, a retry loop that hides two extra calls, or an agent that retrieves the same document five times all look fine per call and terrible per outcome.
Log per request: input tokens, cache-read and cache-write tokens, output tokens, model, latency, and a task id that ties retries and sub-calls to one unit of work. Then track:
Set a per-task budget and alert on breach. Agentic systems fail expensively in loops, and an unbounded retry is a bill, not an error.