Overview
When tools produce large outputs (e.g., API responses, file contents), they can flood the context window. Dynamic Context Discovery:- Intercepts tool outputs via middleware
- Evaluates size against configurable threshold
- Queues large outputs to artifact storage
- Returns compact references in context
- Provides tools for on-demand retrieval (tail, grep, chunk)
Quick Start
Configuration
Basic Setup
Custom Configuration
QueueConfig (Low-Level)
Artifact Tools
When you callctx.get_tools(), these tools are provided to agents:
| Tool | Description |
|---|---|
artifact_tail | Get last N lines of an artifact |
artifact_head | Get first N lines of an artifact |
artifact_grep | Search for pattern in artifact |
artifact_chunk | Get specific line range |
artifact_list | List available artifacts |
Usage in Agent
OutputQueue Middleware
The middleware intercepts tool calls and queues large outputs:Direct Usage
ArtifactRef
When outputs are queued, they’re replaced with anArtifactRef:
History Persistence
Conversation history is saved to artifacts for loss recovery:Secret Redaction
Sensitive data is automatically redacted in artifacts:- OpenAI keys (
sk-...) - API keys, passwords, tokens
- GitHub tokens (
ghp_...,gho_...) - Slack tokens (
xox...)
Environment Variables
Complete Example
API Reference
setup_dynamic_context()
DynamicContextSetup
| Property | Description |
|---|---|
artifact_store | FileSystemArtifactStore instance |
output_queue | OutputQueue instance |
history_store | HistoryStore instance |
terminal_logger | TerminalLogger instance |
get_tools() | Returns list of artifact tools |
get_middleware() | Returns queue middleware |
OutputQueue
| Method | Description |
|---|---|
process(content, metadata) | Queue if large, else return as-is |
should_queue(content) | Check if content exceeds threshold |
See Also
- Middleware System - Hook-based middleware
- Context Security - Secret redaction
- Context Management - Overview

