Optimizing Agent Context Structure Saves Developer $800/Month in Token Costs
Final_Act_9658 · reddit · 2026-07-30
A developer shared their open-source project, ContextOps, which optimizes LLM context structures to save around $800/month in token costs without losing reasoning capabilities.
Key Findings & Practices:
- Duplicate Injection is the Culprit: In a coding agent test, a 2,500-line file was injected five separate times, ballooning the prompt to over 31,000 tokens.
- Blind Truncation Breaks Logic: Replacing long outputs with [Tool Output: Truncated] dropped tokens to 2,800, but the model completely lost track of the conversation's structure and gave wrong answers.
- Deduplication & Structure Preservation: Keeping the first file occurrence and replacing subsequent duplicates with a short note reduced tokens to 9,081 (a 21k+ cut) while maintaining perfect model accuracy.
- RAG System Optimization: In a 15-document retrieval test, stripping redundant XML wrappers and repeated template intros shrunk the context from 4,941 tokens to 1,941 tokens (60% smaller) without affecting the model's ability to answer complex multi-document questions.
The author concludes that not all tokens are equally valuable. Identifying and removing repeated boilerplate and tool outputs is the most effective way to reduce costs in current Agent and RAG development.
More from coding & agent
- How Test Harness Design Impacts ARC-AGI-3 Scores — dkundel · 2026-07-30
- Peated's 'Run the Website for Me' AI Agent Now Can Suggest Changes Like Duplicate Detection — zeeg · 2026-07-30
- Daily Reading List: Agent Delegation Limits and Sandboxing Untrusted AI Code — rseroter · 2026-07-30
- AI Bug Fixes Often Incomplete, Creating Messy Open Source Security — curious_vii · 2026-07-30
- Codex Tip: Turn Prompts into Repeatable, Scriptable Workflows — reach_vb · 2026-07-30
- ShadKit Open Source: Porting shadcn/ui and Vercel AI Elements to SwiftUI — jasonkneen · 2026-07-30