Is your AI code review bot suddenly costing more than a small nation’s GDP? Yeah, mine too. What started as a lean, mean, diff-analyzing machine — just the system prompt and the actual changes — morphed into a voracious beast gorging on READMEs, CONTRIBUTING guides, style manuals, the last 10 reviews for ‘tone,’ and even source files. Six months later, nobody could quite remember what was in the prompt anymore, but the bill? That was crystal clear: around $0.066 per PR. At 1,500 PRs a month, it adds up. Worse, the bot’s context window was so crammed with irrelevant fluff that it started commenting on decade-old // TODO notes. Classic.
Then came the real kicker. A developer, in a moment of pure debugging bliss, pasted a live Anthropic API key into a docstring. They forgot to remove it. The bot, bless its ever-expanding heart, dutifully shipped that sensitive data right to Anthropic. We caught it, thankfully, but it was the kind of silent, easily repeatable disaster that keeps you up at night. This pattern, this uncontrolled ballooning of AI context and the associated costs and security risks, is a tale as old as time in Silicon Valley — or at least, as old as the last few years of AI hype. Every team I’ve worked with has cobbled together some ad-hoc system to manage it, and it’s always been, frankly, a mess.
The “Everything and the Kitchen Sink” Prompt Problem
This isn’t just about spending money; it’s about efficiency. When you’re shoveling thousands of tokens into an AI model for every single request, most of that data is just noise. It dilutes the signal, making it harder for the AI to focus on what actually matters: the code itself. And when your context window is so stuffed with static documentation that it pushes out the actual diff you need reviewed? You’ve got a problem. A big, expensive problem. The latency creeping up to 8 seconds per review was just the cherry on top of this particular Sundae of Suck.
Enter ctxbudgeter: Taming the Prompt Beast
This is where a neat little Python package called ctxbudgeter comes into play. It’s framework-agnostic, which is always a good sign for wider applicability, and its whole schtick is to compile clean, cheap, and auditable context before it ever hits the model. Think of it as a pre-flight checklist for your AI’s brain. After six months of prompt bloat and a near-disaster, the author spent a week integrating it into their code-review bot. The results? Immediate.
Here’s how they started:
pip install "ctxbudgeter[tiktoken,yaml]"
The core refactoring involved changing the bot’s build_prompt() function into a build_pack() that crafts a typed ContextPack. This allows for explicit control over what goes into the prompt and how it’s prioritized.
```python from ctxbudgeter import ContextPack
def build_pack(pr_diff: str