Context Window Manager That Keeps Pinned Messages Under a Token Budget

Last updated: November 29, 2026

Quick Overview

Design a class that holds a chat conversation and, before each model call, selects which messages to send so the total stays under the context limit minus a reply reserve. The system prompt, pinned messages, and the latest user message must always be included, and the most recent history should be kept first.

OpenAI
Software Engineering Fundamentals
Software Engineer
OpenAI
November 29, 2026
Software Engineer
Technical Screen
Software Engineering Fundamentals
Medium

0

0

0 solved


Design a class that holds a chat conversation and, before each model call, selects which messages to send so the total stays under the context limit minus a reply reserve. The system prompt, pinned messages, and the latest user message must always be included, and the most recent history should be kept first.

Every ChatGPT and Assistants style integration has to fit a growing conversation into a finite context window. The interviewer is looking for a clean object model, correct budget accounting including per-message overhead, and sensible policies when even the required messages do not fit.

What the Interviewer Expects
  • Separates message data from the selection policy
  • Accounts for a reply reserve and per-message token overhead
  • Always keeps system, pinned, and latest user messages
  • Fills remaining budget from newest to oldest and preserves original order
  • Fails explicitly when required content alone exceeds the budget
  • Injects the tokenizer so tests do not need a real one
Key Topics to Cover
object-oriented design
token budgeting
truncation policy
dependency injection
LLM context windows
How to Approach This
  1. Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
  2. Choose data structures based on access patterns, not familiarity.
  3. Prefer immutable data and message passing over shared mutable state for concurrency.
  4. Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
  • How would you summarize dropped history instead of discarding it?
  • How would you avoid sending an assistant reply without the user turn it answered?
  • How do tool call and tool result messages constrain what can be dropped?
  • How would you cache token counts so build stays fast on long chats?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice System Design Problems
Sample Answer
Requirements
  • add(message) appends; build() returns the messages to send, in original order.
  • Budget = max_tokens - reserve_for_reply. Each message costs its token count plus a small framing overhead.
  • Mu...
High-Level Design

A Message dataclass holds role, content, and a pinned flag. ContextWindow owns the list and the policy, with the tokenizer passed in as a callable so production uses the real tokenizer and tests...


Submit Your Answer
Markdown supported