Stateful Tracking in Agent Loops
Maintain explicit state of what the agent already knows so it does not re-fetch information that was retrieved in a prior turn unless it has changed.
How It Works
A lightweight state object tracks file contents, directory listings, tool results, and user preferences across turns. Before making a tool call, the agent checks its state to see if the information is already available and still fresh.
What to Track
- file contents and modification times
- directory listings
- previous tool outputs
- user preferences and constraints
- the current plan and completed steps
Benefits
- eliminates redundant reads
- reduces prompt size by avoiding re-inclusion of unchanged data
- prevents the agent from repeating work
Anti-patterns
- Re-reading a file after every edit to "confirm" the change.
- Sending the full directory tree on every turn instead of caching it.
- Re-fetching tool definitions or repository context that has not changed.
See Also
- Operation Batching — state tracking enables more effective batching by knowing what is already cached.
- Incremental Plan Updates — plan state is a key component of agent state.
- Diff-Based Tool Output — tracking file state enables diff computation.
- Structured State Extraction — takes this further by discarding the transcript entirely in favor of a schema-validated state object.
- Event-Driven Waiting — tracking what is already in flight prevents a re-dispatch after a spurious timeout.