In the last video, Ravi’s thirty-message practice session sent 162 input tokens on its first turn and 4,206 on its last. The session cost four times what its first turn suggested, and continuing it would eventually fill the model’s context window.
In this lesson, you’ll give the history a token budget: a number of input tokens to keep each request under. Before each call, the partner drops the oldest turns until its estimate of the request fits, and you’ll see the cost per turn in Ravi’s session level off. You’ll also weigh the other way to shrink a history, summarizing it. First, though, the partner needs to know how many tokens each turn takes.
Give Every Turn a Token Figure
The provider reports one input total per request, and only after the call. To decide what to drop before a call, the partner needs a figure for every turn, so each turn gets a tokens property next to its role and content.
Most figures can come from usage. Some text hasn’t been counted by the provider yet, though: the instructions, the new message, and a message whose call never finished. For those, you can use a rough rule of about four characters per token. Create history.js next to partner.js, starting with that rule:
history.js
export function estimate(text) {
return Math.ceil(text.length / 4);
}
function sumTokens(turns) {
let total = 0;
for (const turn of turns) total += turn.tokens;
return total;
}
The estimate() function divides the length by four and rounds up with Math.ceil(). It’s an estimate, not a count: each model splits text into tokens its own way, and the only real count is the one in usage. The helper sumTokens() adds up the figures for a list of turns.
The instructions get one fixed figure, estimate(prompt), and they’re never dropped, because the partner can’t work without them. The partner works out every other figure from usage once a call finishes. Add recordTurn() to the end of history.js:
history.js
export function recordTurn(
history,
{ fixed, answer, reply }
) {
if (answer.usage === null) return;
const sent = history.at(-1);
const earlier = sumTokens(history.slice(0, -1));
const left = answer.usage.inputTokens - fixed - earlier;
sent.tokens = Math.max(0, left);
if (!answer.cutOff) {
history.push({
role: "assistant",
content: reply,
tokens: answer.usage.outputTokens,
});
}
}
The script pushes each member message with its estimate, and once the call finishes, recordTurn() replaces that figure. The request’s inputTokens counts everything sent. Take away the instructions’ figure, fixed, and the figures of the turns sent before the new message, and what’s left belongs to the new message. The reply’s figure is its outputTokens.
Why can left come out below zero? Because the earlier figures can be too high: the instructions’ estimate can overshoot, and a reasoning model, like OpenAI’s fast model in this course, counts hidden reasoning in a reply’s output tokens. Anthropic models do too with extended thinking, which this course leaves off. That reasoning never goes back as input. A negative figure makes no sense, so Math.max() floors it at 0.
So the figures split each request’s real total between its turns, and none of them is exact. The first message’s figure absorbs the error in the instructions’ estimate, and each later message takes what’s left after the figures before it, without changing them. When the floor kicks in, the figures add up to more than the request held, and the partner may drop a turn sooner than it needed to.
The first line handles a call that never finished. A reply that’s stopped, or broken off by an error, isn’t added to the history, and the module reports no usage for it. The member message stays in the history as a lone turn, with its estimate as its figure. A reply cut off at the output limit stays out too, as in the earlier lessons, but its usage still sets the member message’s figure.