Chapter 17 — Cost Engineering
TL;DR: An agent running 1 million times a month is not cheap by default. Cost engineering is about making every token count.
Optimization axes
Section titled “Optimization axes”- Reduce tokens per turn — prompt caching, summaries, context pruning
- Model cheaper — Haiku vs Sonnet vs Opus
- Batch + defer — group requests offline when possible
// Before: cache disabledasync function agentExpensive(query: string) { return await llm.generate({prompt: systemPrompt + query});}
// After: cache enabled, context prunedasync function agentOptimized(userId: string, query: string) { const cached = { type: "text", text: systemPrompt, cache_control: { type: "ephemeral" } };
const profile = await pruneProfile(userId); // summary, not full
return await llm.generate({ messages: [ {role: "user", content: [cached, {type: "text", text: profile + query}]} ] });}