方寸 Portal

AI Engineer Portal

Your personal operating system for career transition.

Private mode

Exercise

Conversation memory manager with token budgeting

Conversation Memory Manager with Token Budgeting

Agents that run multi-turn conversations or long-running tasks quickly blow through context windows. A memory manager solves this by keeping the most important context within a token budget, using a combination of sliding window (keep recent messages) and summary compression (condense older messages into a summary).

What you are building

Create a MemoryManager class that:

  1. Tracks conversation history — stores messages with role, content, and token counts.
  2. Enforces a token budget — when adding a message would exceed the budget, compress older messages.
  3. Uses sliding window — always keeps the N most recent messages verbatim.
  4. Compresses with summaries — when messages are evicted from the window, they are compressed into a running summary using a provided summarizer function.
  5. Preserves system messages — the system prompt is never evicted or compressed.
  6. Provides a get_messages method — returns the current conversation formatted for an LLM API call: system message + summary (if any) + recent window.

Why this matters

Token management is one of the most important and least glamorous parts of agent engineering. Without it, agents either crash with context-too-long errors or silently lose important context. The sliding window + summary pattern is used by virtually every production agent that handles multi-turn conversations.

Understanding token budgeting also helps you reason about cost: if your agent uses 100K tokens per conversation, that is real money at scale.

Constraints

  • Use a provided count_tokens(text) -> int function (simulated as len(text.split()) for this exercise).
  • Use a provided summarize(messages) -> str function for compression.
  • The system message budget is separate from the conversation budget.
  • When the summary itself exceeds 25% of the budget, re-summarize it (recursive compression).

Agents / advanced / Step 5 of 8

Practice stage

Agent architecture patterns

Hint

Focus on explicit control surfaces — function schemas, state machines, and structured outputs. Agents are most useful when the task requires dynamic tool selection or multi-step reasoning. Start with the simplest pattern (single tool call) before reaching for ReAct loops.

Success criteria
  • - Tools return structured, typed responses
  • - Agent completes the task within a bounded number of steps
  • - All tool calls include error handling and retries
  • - Memory/state management prevents unbounded context growth
Review checklist
  • - Tool schemas validate inputs and handle errors gracefully
  • - Agent loop has explicit termination conditions
  • - State is serializable and inspectable between steps
  • - Cost and token usage are tracked per invocation
  • - Fallback behavior exists for tool call failures

Practice

Generate a variation

Generate a new exercise variation to deepen understanding or practice a related concept.

Attempt history

Recent submissions

Before you submit, decide what a strong answer should make obvious to the reviewer.

No attempts yet.