Measure the Context Before You Touch the Prompt
Most blown budgets start before the first message is sent. The biggest variable in a coding session is not the answer you want back, it is the context you hand over: repository files, a stack trace, a diff, the chat history from the turn before, and every tool result that gets folded back into the prompt. Estimate that pile first and the rest of the math falls into place.
A rough tokenizer is enough for a first pass. English prose averages about four characters per token, and source code usually lands closer to three, so take the character count of every file you plan to include and divide by three and a half. A two hundred line module that you paste in full might run two thousand to three thousand tokens. Add the system prompt, tool schemas, and instructions your agent injects on its own, because that overhead arrives whether you asked for it or not.
Now estimate the session at its end, not at its start. Every turn re-sends the accumulated context, so a conversation that begins at fifteen thousand tokens can finish much heavier after a few tool calls and file reads. Ask yourself how many turns the task realistically needs, then price the run as if the final turn carries the full weight of everything that came before it.
Write Down a Hard Token Ceiling
An estimate without a stop rule is a wish. Decide the maximum token count you are willing to spend before you begin, write it down in your notes, and treat it as a boundary the run does not cross. If the task is exploratory, set the ceiling lower than you think you need; you can always start a second, better scoped run once you know more about the problem.
Split the ceiling into input and output. Output tokens are the expensive side of most runs, so a rambling answer costs more than a tight one even when the prompt is identical. Cap the response length where the tool allows it, and tell the model to return the code and skip the narration. On long refactors, ask for the changed regions instead of whole files and the savings compound across turns.
Add a deliberate buffer. A calm, well scoped task might finish near its estimate; an unfamiliar codebase rarely does. Multiply your rough number by one and a half for routine edits and by two when you are debugging something you have never seen before. The buffer is not slack to spend casually, it is the margin that keeps you from stopping halfway through a fix with the problem still open.
Match the Model to the Job You Priced
The per-token rate is only half the equation, because different models reach a usable answer with different amounts of back and forth. A capable model that solves a multi-file refactor in three turns can cost less overall than a cheaper one that needs ten. Price the whole run, not the individual request.
For heavyweight work such as cross-module refactors and unfamiliar architecture, Claude Sonnet 5.5 is a sensible choice, and its reasoning tends to hold up over long contexts. For routine edits like test fixes, formatting sweeps, and small mechanical changes, DeepSeek V4 Flash keeps the run inexpensive without adding turns.
A two-tier habit helps. Use a lighter model to scope the problem, list the files involved, and draft an approach, then hand the hard core to the stronger model with that briefing already written down. You pay for the cheap exploratory turns and reserve the expensive ones for the part that actually needs them.
Whatever you choose, pin the exact model name before the run starts. Switching models mid-session breaks the estimate, because the rate changes underneath a budget you already committed to.
Run This Checklist Before the Green Light
Nothing above matters if the wiring is wrong, so spend two minutes on the setup itself. The full pre-flight pass looks like this:
- Confirm the endpoint. Claude Code and Gemini CLI take the bare host, api.fastapirelay.com. OpenAI-compatible SDKs add /v1 to that base URL.
- Pin the model name exactly as your provider lists it, and keep it visible in your notes next to the budget.
- Check that your API key is loaded and that the remaining credit covers your ceiling with the buffer included.
- Snapshot the context. Note which files, diffs, and logs you plan to feed in, with a rough token count for each.
- Set the output cap and a stop condition, so the run ends at a known point instead of drifting.
- Send a tiny probe request first. A one-line prompt confirms the key, the endpoint, and the model name before you spend anything meaningful.
- Record the starting token count and the time, so the after-action review has something to compare against.
- Decide the escape hatch: what you will do if the estimate is already half spent and the task is not done.
That probe request deserves its own emphasis. It catches the two mistakes that ruin an estimate before it begins: a misconfigured host and a misspelled model name. Both silently route your tokens somewhere you did not budget for.
Compare the Actuals Against Your Guess
When the run ends, open the usage numbers and write down the real input and output totals beside your estimate. The ratio between the two is the most useful number in this whole exercise. If you guessed twice as high as reality twice in a row, your per-file estimates are too generous; if you ran past the ceiling, the buffer was too thin or the scope grew mid-task.
Keep the comparison in a small table in whatever notes tool you use: task type, estimated tokens, actual tokens, model, number of turns, and a one-line note on what surprised you. After five or six rows, you stop guessing and start recognizing patterns, like how a flaky test investigation always costs triple a straightforward feature change.
The habits are cheap to build and they compound. A measured run teaches you more about your own workflow than any default setting, and it turns budgeting from a worry into a routine you run before every task. If you do not have an account yet, sign up for SwiftRelay and spend the small gift credit on your first measured run, then invite a friend when you are ready, since their top-ups add a standing share to yours.