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

# Dynamic Context Discovery

> Artifact-based storage for large tool outputs

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

agent = Agent(
    name="researcher",
    instructions="Use artifact tools when tool output is large.",
)
agent.start("Fetch the full API schema and summarise the auth section.")
```

Large tool outputs are queued to artifacts automatically — the agent sees compact references instead of flooding context, and explores data on demand with artifact tools.

The user asks for large data; middleware stores bulky tool output as artifacts and keeps compact references in context.

```mermaid theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
graph LR
    T[Large Tool Output] --> M[Middleware]
    M --> A[Artifact Store]
    M --> R[Compact Ref in Context]
    R --> Agent[Agent]
    Agent --> G[artifact_grep / tail]

    classDef input fill:#8B0000,stroke:#7C90A0,color:#fff
    classDef process fill:#189AB4,stroke:#7C90A0,color:#fff
    classDef store fill:#6366F1,stroke:#7C90A0,color:#fff
    classDef output fill:#10B981,stroke:#7C90A0,color:#fff

    class T input
    class M process
    class A store
    class R,Agent,G output

```

## Quick Start

<Steps>
  <Step title="Enable dynamic context on an agent">
    ```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(
        name="Analyst",
        instructions="You are a data analyst.",
        tools=ctx.get_tools(),
        hooks=[ctx.get_middleware()],
    )

    agent.start("Fetch and analyse the large dataset")
    ```

    Outputs over 32 KB are saved as artifacts; the agent receives references and uses `artifact_tail`, `artifact_grep`, etc.
  </Step>

  <Step title="Tune the inline threshold">
    ```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
    ctx = setup_dynamic_context(
        inline_max_kb=16,
        redact_secrets=True,
        history_enabled=True,
    )
    ```
  </Step>
</Steps>

***

## How It Works

```mermaid theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
sequenceDiagram
    participant Tool
    participant Middleware
    participant Store as Artifact store
    participant Agent

    Tool->>Middleware: large output (> inline_max_kb)
    Middleware->>Store: save artifact
    Middleware->>Agent: compact reference in context
    Agent->>Store: artifact_grep / artifact_tail
    Store-->>Agent: targeted slice
```

Outputs above `inline_max_kb` are written to disk under `base_dir`. The agent sees a short reference and pulls only the lines it needs via artifact tools — context stays bounded even after heavy tool runs.

***

## Available Tools

| Tool             | Description        |
| ---------------- | ------------------ |
| `artifact_tail`  | Last N lines       |
| `artifact_head`  | First N lines      |
| `artifact_grep`  | Search for pattern |
| `artifact_chunk` | Line range         |
| `artifact_list`  | List artifacts     |

## Configuration

| Parameter         | Default             | Description                    |
| ----------------- | ------------------- | ------------------------------ |
| `inline_max_kb`   | `32`                | Queue outputs larger than this |
| `redact_secrets`  | `True`              | Redact API keys and passwords  |
| `history_enabled` | `True`              | Persist conversation history   |
| `base_dir`        | `~/.praisonai/runs` | Artifact storage directory     |

Environment variables: `PRAISONAI_ARTIFACT_DIR`, `PRAISONAI_ARTIFACT_INLINE_MAX_KB`, `PRAISONAI_ARTIFACT_REDACT`.

## Best Practices

<AccordionGroup>
  <Accordion title="Lower inline_max_kb for memory-heavy agents">
    16 KB keeps more headroom in context for long multi-tool sessions.
  </Accordion>

  <Accordion title="Keep redact_secrets enabled in production">
    Automatic redaction prevents credentials in tool output from reaching the LLM or disk logs.
  </Accordion>

  <Accordion title="Use artifact_grep before loading full files">
    Search artifacts first — avoid pulling entire multi-MB outputs into a single prompt.
  </Accordion>
</AccordionGroup>

## Related

<CardGroup cols={2}>
  <Card title="Context Management" icon="layer-group" href="/docs/features/context-management">
    Full context system
  </Card>

  <Card title="Security & Redaction" icon="shield" href="/docs/features/context-security-redaction">
    Secret redaction details
  </Card>
</CardGroup>
