I've been running LLM agents inside CI for a few months now (test triage, codemod PRs, the usual), and the thing that keeps biting me isn't a wrong answer — it's the bill. A long agent run silently gets expensive, and you find out after the job finishes, not before. One background agent looping on a flaky test turned a "cheap" PR check into a real line item, and nobody noticed until the invoice.

What's actually helped, tool-agnostic:

  • Budget the prompt before the run, not after. Estimate the token cost of the context you're about to send and fail fast if it's absurd. Same reflex as refusing to git push a 2GB blob.
  • Attribute tokens per-file / per-step. When one step is 80% of spend, you want to see that, the same way you'd profile a slow build stage.
  • Gate the cost in CI the way you'd gate build time. We have wall-clock budgets and bundle-size budgets that fail the check when they regress — a token/cost budget that fails the PR is the same idea, just a different resource.

That third one is the mental model I wish I'd had a year ago: treat the context window like a CI cost gate, not like free scratch space.

So — genuine question for the room: how do you stop an agent PR from silently 10×-ing token spend? Do you cap it at the provider dashboard, in the CI config, in a wrapper script? Curious what's actually working for people running this in anger.