Skip to content

Never split a pair

Read · 1 of 2

Calls and results travel together

Every tool_result must follow the tool_use it answers. If trimming drops the call but keeps the result, the API refuses the request.

So treat a call and its result as one unit when trimming: keep both, or drop both.

Read this lesson as text

A tool result without the call it answers breaks the conversation.

Calls and results travel together

Every tool_result must follow the tool_use it answers. If trimming drops the call but keeps the result, the API refuses the request.

So treat a call and its result as one unit when trimming: keep both, or drop both.

Exercise: Trim in pairs

Same as before, but each line is id tokens kind where kind is msg, call or result. A result always comes right after its call.

Keep the goal (the first line). Walk back from the newest unit, treating each call+result as one unit; stop at the first unit that doesn't fit.

Print the kept ids in order, then tokens=T.