Log inBook a Demo
BuildMidData / MLAuto-graded

Trim a conversation to fit the context window

An explicit priority order has to be honoured exactly: what is never dropped, what goes first, when to refuse.A conversation grows forever; the context window does not. Before every model call you have to choose which messages to send so the prompt fits the budget — without dropping the system instructions and without discarding the recent turns the user actually cares about. The shipped trim_to_budget gets this backwards: it keeps the oldest messages, drops the newest, ignores the system message, and can still overflow.
30 min
python

You're done when

  • The result always fits within the budget.
  • The system message is retained rather than trimmed away.
  • Recent messages are kept and the oldest are dropped, not the other way round.
  • The remaining messages stay in their original order.
  • When everything already fits, nothing is dropped.
  • Multiple system messages are all retained.
  • When even the system messages cannot fit, a budget error is raised rather than silently overflowing.

What it measures

Context engineeringDeciding what earns space in a finite context window, and what is safe to lose.
Prompt engineeringPreserving the instructions and the recent turns that determine whether the next response is any good.
Impossible-case handlingRaising clearly when the budget cannot be met, instead of returning something that will fail at the API.

Your environment

Terminal + editorA full shell in the browser
Claude CodePre-authenticated, no key setup
Isolated microVMYours alone, wiped afterwards

Start this lab

30 min
Start labYou'll sign in to Anthropos to start. The lab opens in a private cloud workspace with Claude Code ready to use.
AI Labs is part of Anthropos Workforce. Not on your plan yet? Book a demo