Reduce LLM token costs with 50 context-engineering techniques, RAG, caching, Graphify, and code retrieval.
Where is your LLM spend actually going, and what would cut it without hurting output quality?
The fastest way to reduce LLM token usage is context engineering, not shorter prompts. Retrieve only high-signal chunks, cache repeated system context, send code structure instead of full files, and compress conversation history. Teams using knowledge graphs like Graphify often see the largest savings on code-heavy workflows.
The biggest gains rarely come from trimming a few words off a prompt. They come from context engineering: retrieving only the smallest high-signal context needed for the task, avoiding repeated transmission of the same information, and using structured representations rather than raw text.
Graphify and structural context
Graphify transforms your codebase into a deterministic knowledge graph. Instead of sending thousands of lines of code to the LLM every time, the AI navigates the graph and loads only what’s relevant. See Setup Your Coding Assistant for Low LLM Token Usage for installation on Windows, macOS, and Linux.
That’s structural context optimization, one piece of a larger playbook. Below are 50 additional ways to reduce LLM token usage, organized by category.
1. Prompt Engineering
- Remove unnecessary words and explanations.
- Replace paragraphs with bullet points.
- Use consistent abbreviations.
- Remove repeated instructions.
- Move permanent instructions into the system prompt.
- Reference previous answers instead of repeating them.
- Use IDs instead of long names.
- Request structured JSON instead of verbose prose.
- Limit output length explicitly.
- Avoid examples unless necessary.
2. Conversation Management
- Summarize conversations every few turns.
- Keep only the relevant chat history.
- Compress old messages into memory.
- Store conversation state externally.
- Replace long history with structured facts.
- Remove greetings and pleasantries.
- Delete failed attempts.
- Reset context after task completion.
- Split unrelated topics into separate chats.
- Store reusable facts in a profile.
3. RAG Optimization
- Retrieve only the top-ranked chunks.
- Rerank before sending context.
- Merge overlapping chunks.
- Remove duplicate passages.
- Extract only relevant paragraphs.
- Compress retrieved documents.
- Filter by metadata first.
- Retrieve summaries before full documents.
- Use hierarchical retrieval.
- Retrieve only changed documents.
4. Code Context Optimization
- Send ASTs instead of full source files.
- Load only relevant functions.
- Use symbol lookup.
- Send interface definitions only.
- Replace implementations with signatures.
- Send Git diffs instead of entire files.
- Load dependency graphs first.
- Use semantic indexes such as Graphify.
- Reference file paths instead of embedding file contents.
- Cache parsed project metadata.
5. Data Compression
- Use compact data formats.
- Remove unnecessary whitespace.
- Shorten property names.
- Deduplicate repeated values.
- Encode repeated structures once.
- Replace repeated text with references.
- Convert tables to CSV.
- Use domain-specific mini languages (DSLs).
- Send hashes or IDs for unchanged data.
- Store large assets externally and retrieve them only on demand.
Highest-Impact Techniques
| Technique | Typical impact | When to use |
|---|---|---|
| Knowledge graph retrieval (Graphify) | Largest on large codebases | Multi-file refactors, architecture questions |
| AST / symbol retrieval | High on code tasks | Bug fixes, API lookups |
| Context summarization | High on long chats | Support bots, multi-turn agents |
| Prompt caching | High on repeated prompts | Stable system prompts, doc Q&A |
| RAG reranking | Medium–high | Document search pipelines |
| Conversation compression | Medium–high | Long agent sessions |
| Delta prompting | High on iterative edits | Code apply/review loops |
| Structured JSON output | Medium | API extraction, tool calls |
The biggest cost reductions come from sending dramatically less context, not slightly shorter prompts. Start with retrieval and caching before fine-tuning wording.
Advanced patterns (beyond the 50)
Teams building production AI applications often combine:
- Prompt and semantic caching
- Delta prompting (send only changes since last turn)
- Hybrid retrieval (BM25 + embeddings)
- Model routing to cheaper models for simple steps
- Multi-stage pipelines with summarization between stages
- Entity extraction to replace long passages with structured facts
- Dynamic context windows sized per task type
Frequently Asked Questions
What is the fastest way to reduce LLM token usage?
Use context engineering: RAG with reranking, knowledge graphs (Graphify), AST-based code lookup, conversation summarization, and prompt caching for repeated static context.
What is context engineering?
Selecting, compressing, and structuring information sent to the model per task, instead of dumping entire documents or repositories into every request.
Does prompt caching reduce LLM costs?
Yes. Providers with prompt caching charge less for repeated system prompts and static context, often 50–90% on cache hits when combined with stable system instructions.
How does Graphify reduce tokens for coding assistants?
It indexes code structure so assistants query symbols and dependencies instead of re-reading full files, see the setup guide.
RAG vs full documents: which uses fewer tokens?
RAG usually wins by injecting only top-ranked chunks. Optimize with metadata filters, reranking, and hierarchical retrieval (summaries first).
One lesson from production AI systems: cost drops when context drops. The future of efficient AI is context engineering, not prompt trivia.
Related Reading
- Setup Your Coding Assistant for Low LLM Token Usage
- 22 Coding Assistants and Their Capabilities
- 86 Most Popular Open Source AI Tools
Contact Sea Wing AI to optimize LLM costs across your development organization.