
Multi-Turn Management in Agents: State vs. Memory
Master the complexities of long-running autonomous tasks. Learn how to manage 'Plan Drift', optimize agent state, and prevent 'Loop Exhaustion' through efficient token turns.
Multi-Turn Management in Agents: State vs. Memory
An agent is more than just a chatbot. It performs a series of Turns (Step 1 -> Step 2 -> Step 3...) to achieve a goal. Each turn consumes tokens. If an agent repeats itself or fails to "Compress" its progress, the Token Accumulation can lead to a crash before the task is finished.
In this lesson, we master Multi-Turn Management. We’ll move from "Simple Persistence" to "Differential State Persistence." We will learn how to distinguish between the agent's Reasoning History (which can be pruned) and its Action State (which must be exact).
1. reasoning History vs. Action State
- Reasoning History: The "Internal Monologue" (Module 10.4). Why the agent chose a tool. It is useful for turn $N+1$ but often useless for turn $N+10$.
- Action State: The Facts gathered. The data retrieved from a database. This is the Gold that the agent must carry to the finish line.
Efficiency Strategy: Use Reasoning Eviction. Delete or summarize "Thoughts" from 5 turns ago, but keep all "Tool Results" (Facts) verbatim.
2. Preventing "Plan Drift"
As agents consume more tokens, they experience Context Dilution. They forget the original goal or start hallucinating new instructions.
The "Goal Anchor" Pattern
Include the Original User Query at the bottom of every system prompt in every turn. This "Anchors" the model's attention to the exit condition, preventing it from wandering into expensive, irrelevant logic loops.
3. Implementation: The State Trimmer (LangGraph)
In LangGraph, you define a State object. If you aren't careful, every node will append its full log to the state.
Python Code: Differential State Management
from typing import TypedDict, List
class AgentState(TypedDict):
plan: List[str]
facts: List[str]
# We maintain a 'RecentThoughts' list,
# not a 'GlobalHistory' list.
recent_thoughts: List[str]
def reasoning_node(state: AgentState):
# 1. Take the new thought
new_thought = "I determined the server is down."
# 2. Add it to the list
new_recent = state['recent_thoughts'] + [new_thought]
# 3. TRUNCATE the older thoughts (Keep only last 3)
# This prevents the token count from exploding in a multi-turn loop
return {"recent_thoughts": new_recent[-3:]}
4. The "Checkpoint and Resume" Strategy
For very long tasks (e.g. "Research and write a 50-page report"), do not use one long agent session.
- Break it down: Turn the task into 5 smaller sub-tasks.
- Execute and Save: Run Agent 1, save the result to a JSON file, and Kill the Session.
- Resume with Context: Start Agent 2 with ONLY the 500-token summary of Agent 1's work as its input.
Savings: You effectively reset the "Input Token" multiplier at every sub-task boundary.
5. Visualizing the "Token Slope"
In your React UI, track the Cumulative Token Count of an agentic session.
graph TD
T1[Turn 1: 500 tokens] --> T2[Turn 2: 1,200 tokens]
T2 --> T3[Turn 3: 2,500 tokens]
T3 -->|TRUNCATE| T4[Turn 4: 800 tokens]
T4 --> T5[Turn 5: 1,500 tokens]
style T3 fill:#f66
style T4 fill:#6f6
If the slope is too steep, it means your agent is not pruning its memory correctly.
6. Summary and Key Takeaways
- Evict Reasoning, Keep Facts: Thoughts are temporary; data is permanent.
- Goal Anchoring: Always remind the agent of the target to prevent "Loop Drift."
- State Truncation: Use sliding windows for "Internal Monologue" in LangGraph.
- Sub-Task Boundaries: Kill and restart sessions between major logical leaps to reset context costs.
Exercise: The Agent Audit
- Run an agent (e.g. using a "Search" tool) to find the current price of 5 different stocks.
- Observe the Total Tokens sent in the final Turn (where it summarizes everything).
- Apply the 'Reasoning Eviction' pattern: Modify the code to delete the "Step 1: I am searching..." "Step 2: I am searching..." text before sending the final turn.
- How many tokens did you save?
- Check for Accuracy: Does the agent still know all 5 prices? (Yes, because prices are Facts, not Thoughts).