4.5K
Context & Memory emerging

Session-Scoped Context Runtime for Agent Tools

Interpose a context runtime that caches structured reads and normalizes tool output so sessions reuse compact representations instead of repeating raw tokens.

By Yves Gugger (@yvgude)
Add to Pack
or

Saved locally in this browser for now.

Cite This Pattern
APA
Yves Gugger (@yvgude) (2026). Session-Scoped Context Runtime for Agent Tools. In *Awesome Agentic Patterns*. Retrieved May 7, 2026, from https://agentic-patterns.com/patterns/session-scoped-context-runtime-for-agent-tools
BibTeX
@misc{agentic_patterns_session-scoped-context-runtime-for-agent-tools,
  title = {Session-Scoped Context Runtime for Agent Tools},
  author = {Yves Gugger (@yvgude)},
  year = {2026},
  howpublished = {\url{https://agentic-patterns.com/patterns/session-scoped-context-runtime-for-agent-tools}},
  note = {Awesome Agentic Patterns}
}
01

Problem

Coding agents read the same files and command outputs many times per session. Each hop typically pastes full text into the model context, so cost and latency grow with repetition and verbosity even when the underlying artifact has not changed.

02

Solution

Introduce a context runtime alongside the agent—commonly as an MCP server—that owns how workspace state enters the model:

  1. Session-scoped cache for read operations with cheap revalidation (for example, file mtime) so identical reads collapse to small cache hits instead of full payloads.
  2. Structured read modes (for example, dependency maps, signatures, diffs, or task-filtered excerpts) so the agent requests the smallest representation that still supports the next decision.
  3. Normalized tool channels so shell and search results pass through compressing adapters where patterns are stable (for example, common git or package-manager shapes).

The runtime sits between the IDE or host and the model: tools call into it first; it returns compact, typed context and records what is already hot in-session.

on_tool_read(path, mode):
  if cache_valid(path): return cache_entry(path, mode)
  ast_or_text = load_and_parse(path)
  projected = project(ast_or_text, mode)  // map | signatures | diff | ...
  store_cache(path, mode, projected)
  return projected
03

How to use it

  • Expose read, search, shell, and tree operations through the runtime so routing is consistent.
  • Default to a neutral automatic mode, then tighten to maps or signatures when the task only needs structure.
  • Invalidate or refresh on explicit edits, branch changes, or when the host signals a new subagent boundary.
  • Pair with existing context hygiene patterns (for example, auto-compaction or minimization) so hot cache entries still respect global window budgets.
04

Trade-offs

  • Pros: Fewer redundant tokens on repeated exploration; easier enforcement of “structure-first” context; can centralize policy for what leaves the workspace.
  • Cons: Additional moving parts and correctness responsibility (cache staleness, mode selection bugs); host integration work; behavior differs from stock tools unless agents are pointed at the runtime consistently.
06

References