Day 4
State and Context
Understand how agents maintain task state across multiple steps.
State management is one of the hardest problems in agent design. Each loop iteration adds tool results and reasoning to the context window. The agent must decide what to keep, what to discard, and how to organise information so it can make sound decisions across the full execution.
Today’s Lesson
Read
Memory
Context budgets, durable state, and retrieval handoffs.
Practice
Apply what you learned by solving these coding problems.
Review
Test yourself with these interview-style questions.
1Describe the FIFO sliding-window approach for agent context management. What are its strengths, its critical weakness, and what mitigation strategies address that weakness? Compare with at least one alternative strategy.+
FIFO sliding window keeps the most recent N entries and drops the oldest when the limit is reached. Its strength is simplicity and O(1) performance — always keeps recent context available. Its critical weakness is that early, important context (like the original goal) can be evicted. Mitigations include: pinning the goal in a non-evictable section, and periodically summarising evicted entries into a compressed header. An alternative is summarisation-based compression, which calls the LLM to condense history into a summary, preserving key information more compactly but with higher cost and risk of information loss.
Read full answer
The FIFO sliding window is the simplest context management strategy: maintain a deque of entries with a running token count, evict from the left when the total exceeds the limit. Its main strength is predictable O(1) behaviour and implementation simplicity. Its critical weakness is that early context, especially the original user goal, may be evicted after enough steps, causing the agent to lose sight of its objective. I have seen this manifest as the agent suddenly asking "What was the original task?" halfway through execution. Two mitigations work well in practice. First, goal pinning: reserve a non-evictable section at the top of the context that always contains the original goal. This adds a fixed token overhead (typically under 100 tokens) regardless of step count. Second, periodic summarisation: every N steps, call the LLM to compress the full context into a brief summary that captures the goal, key findings, and current state. Replace the raw context with the summary. This is more token-efficient but risks losing nuance. An alternative strategy is structured state: maintain a JSON object with well-defined fields like current_goal, findings, pending_tasks. This is more predictable but less flexible and requires schema design upfront.
2An agent starts with the goal "Find and compare prices of three laptops." After 8 steps, it suddenly asks "Which laptop do you want me to find?" The trace shows early steps mentioning the goal are gone from the context. Diagnose the issue and propose two solutions.+
The agent lost its original goal due to context window overflow — the sliding window evicted the initial system prompt stating the goal. Solution one: pin the original goal in a separate, non-evictable context slot so it persists across all steps. Solution two: implement periodic summarisation every N steps that compresses the context (including the goal) into a summary, then replace raw context with the summary. Pinning is simpler but consumes tokens permanently; summarisation is more efficient but risks information loss.
Read full answer
This is context drift from unbounded growth. Each step adds text to the context; around step 8 the accumulated history exceeds the token limit, and the sliding window evicts the oldest entries — including the system prompt with the original goal. The agent then reverts to base behaviour, asking for a goal it already received. Two solutions: first, goal pinning — a reserved context section that is never evicted. The original goal stays in pinned storage across all steps. This is simple but consumes tokens permanently (maybe 50 tokens per step, negligible). Second, periodic summarisation: every 3-5 steps, call the LLM to compress accumulated context into a brief summary that includes the original goal and key findings. Replace raw context with the summary. This is more token-efficient but risks losing nuance during compression. In production, I use a hybrid: pin the original goal and the most recent 2 steps, summarise the middle history.
3Compare sliding-window truncation, summarisation-based compression, and structured state objects for context management. For each, describe a best-case scenario and a failure scenario. Design an adaptive context manager that switches strategies.+
Sliding window is best for short linear tasks (single-turn Q&A) but fails when early context matters later (multi-step research). Summarisation is best for long sessions (day-long assistant) but fails when granular details must be preserved (exact tool outputs for audit). Structured state is best for tasks with well-defined data flows (pipelines) but fails for open-ended creative tasks. An adaptive manager monitors context growth rate, early-reference frequency, and error rates to switch strategies.
Read full answer
Each strategy optimises a different axis. Sliding window is simplest — keep last N tokens, drop the rest. Best for short, linear interactions where only recent context matters. Fails when early information is needed later — common in research and debugging. Summarisation compresses history via LLM calls. Best for long-running assistants where token budget is tight. Fails when the summary omits a detail that becomes important later, like a specific user constraint. Structured state maintains a JSON object with well-defined fields (current_goal, findings, pending_tasks). Best for data-processing pipelines where state shape is pre-defined. Fails for unpredictable tasks like creative writing. For an adaptive manager, I monitor three signals: context growth rate per step, early-reference frequency (how often the LLM mentions early entries), and error rate from repeated questions. When early-reference frequency drops, sliding window suffices. When it stays high, I switch to summarisation. When tasks have clear structure, I suggest structured state as a configuration option.
Back to 30-Day Agentic AI Interview Prep Path