> ## Documentation Index
> Fetch the complete documentation index at: https://docs.praison.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Dynamic Context Discovery

> Artifact-based storage for large tool outputs

Dynamic Context Discovery automatically queues large tool outputs to artifacts, preventing context flooding while enabling on-demand retrieval.

## Quick Start

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
from praisonaiagents import Agent
from praisonai.context import setup_dynamic_context

ctx = setup_dynamic_context()

agent = Agent(
    instructions="You are a data analyst.",
    tools=ctx.get_tools(),
    hooks=[ctx.get_middleware()],
)

response = agent.chat("Fetch and analyze the large dataset")
```

**What happens:**

* Large tool outputs (>32KB) are automatically saved as artifacts
* Agent receives compact references instead of flooding context
* Agent can use `artifact_tail`, `artifact_grep` to explore data on-demand

## Configuration

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
ctx = setup_dynamic_context(
    inline_max_kb=16,       # Queue outputs > 16KB (default: 32)
    redact_secrets=True,    # Auto-redact API keys, passwords
    history_enabled=True,   # Persist conversation history
)
```

## Available Tools

When you pass `ctx.get_tools()`, agents get:

| Tool             | Description        |
| ---------------- | ------------------ |
| `artifact_tail`  | Get last N lines   |
| `artifact_head`  | Get first N lines  |
| `artifact_grep`  | Search for pattern |
| `artifact_chunk` | Get line range     |
| `artifact_list`  | List artifacts     |

## How It Works

```
Tool Output (large) → Middleware → ArtifactStore → ArtifactRef (inline)
                                                          ↓
                                            Agent uses artifact_tail/grep
```

1. Middleware intercepts tool outputs
2. Outputs > threshold are saved to `~/.praisonai/runs/`
3. Compact reference returned in context
4. Agent uses artifact tools to explore data

## Environment Variables

```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
PRAISONAI_ARTIFACT_DIR=~/.praisonai/runs
PRAISONAI_ARTIFACT_INLINE_MAX_KB=32
PRAISONAI_ARTIFACT_REDACT=true
```

## API Reference

### setup\_dynamic\_context()

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
def setup_dynamic_context(
    base_dir: str = "~/.praisonai/runs",
    inline_max_kb: int = 32,
    redact_secrets: bool = True,
    history_enabled: bool = True,
    terminal_logging: bool = False,
) -> DynamicContextSetup
```

### DynamicContextSetup

| Method             | Returns                                       |
| ------------------ | --------------------------------------------- |
| `get_tools()`      | List of artifact tools for `Agent(tools=...)` |
| `get_middleware()` | Queue middleware for `Agent(hooks=...)`       |

## See Also

* [Context Management](/docs/features/context-management)
* [Security & Redaction](/docs/features/context-security-redaction)
