Recursive Context

How PraisonAI handles large context without context rot

Large outputs stay outside the context window — agents explore them with artifacts, delegation, and escalation instead of stuffing everything into one prompt.

python
from praisonaiagents import Agent, AutonomyConfig, ExecutionConfig

agent = Agent(
    instructions="You are a data analyst.",
    autonomy=AutonomyConfig(level="full_auto", max_iterations=30),
    execution=ExecutionConfig(code_execution=True),
)

agent.start("Analyse 5000 tickets and count billing issues")

The user requests analysis on huge data; the agent stores artifacts and delegates instead of overfilling context.

Diagram

Recursive Context

💬 Query

🤖 Agent

📄 Large output

💾 Artifacts

👥 Sub-agent

✅ Answer

How It Works

Diagram
RecursiveContextAgentUserRecursiveContextAgentUserRequestProcessResultResponse

Quick Start

  1. Simple Usage

    Enable autonomy — artifacts, escalation, and doom-loop protection activate automatically:

    python
    from praisonaiagents import Agent
    
    agent = Agent(
        instructions="You are a data analyst.",
        autonomy=True,
    )
    
    agent.start("Summarise the key themes in this support inbox export")
  2. With Configuration

    Tune iteration limits, doom-loop threshold, and code execution:

    python
    from praisonaiagents import Agent, AutonomyConfig, ExecutionConfig
    
    agent = Agent(
        instructions="You are a data analyst.",
        autonomy=AutonomyConfig(
            level="full_auto",
            max_iterations=30,
            doom_loop_threshold=3,
            auto_escalate=True,
        ),
        execution=ExecutionConfig(code_execution=True),
    )
    
    agent.start("Analyse 5000 tickets and count billing issues")

How It Works

Diagram
Recursive ContextAgentUserRecursive ContextAgentUserRequestProcess requestResultResponse

Instead of loading entire documents into the LLM window, PraisonAI stores large outputs as artifacts and lets agents peek, grep, and chunk them on demand. When tasks grow complex, escalation increases capability stage-by-stage; sub-agent delegation handles focused subtasks with scoped budgets.

Diagram

Stage 0
Direct

Stage 1
Heuristic

Stage 2
Planned

Stage 3
Autonomous

ComponentRole
Artifact storeHolds large outputs; agents query with head, grep, chunk
Escalation pipelineProgresses from direct answer → tools → plan → sub-agents
Handoffs & parallel_handoffsRoute work to focused agents, with per-run budgets
Doom loop trackerStops repeated identical actions

Configuration Options

AutonomyConfig

OptionTypeDefaultDescription
levelstr"suggest"Autonomy level (suggest, auto_edit, full_auto)
max_iterationsint20Maximum turns before stopping
doom_loop_thresholdint3Repeated actions before doom-loop stop
auto_escalateboolTrueAutomatically increase complexity stage
modestrautocaller or iterative execution mode

Artifact queueing

Large tool outputs above inline_max_bytes (default 32 KB) are stored as artifacts automatically when queueing is enabled on the agent’s tool configuration.


Common Patterns

Explore a large artifact

Agents with autonomy enabled can inspect stored outputs without loading the full content:

python
from praisonaiagents import Agent, AutonomyConfig

agent = Agent(
    instructions="Search large logs for error patterns.",
    autonomy=AutonomyConfig(level="full_auto"),
)

agent.start("Find all timeout errors in the latest deployment log")

Delegate a subtask

python
from praisonaiagents import Agent, AutonomyConfig

agent = Agent(
    instructions="Coordinate analysis across data sources.",
    autonomy=AutonomyConfig(level="full_auto"),
)

# Delegation runs inside the autonomy loop when escalation reaches Stage 3
agent.start("Count billing questions per region in the ticket export")

Best Practices

Enable autonomy for large or multi-step tasks

Set autonomy=True or AutonomyConfig(level="full_auto") when inputs exceed a few pages or require tool loops. Simple one-shot questions do not need it.

Set doom_loop_threshold for long runs

Keep doom_loop_threshold at 3–5 so agents stop when repeating the same tool call instead of burning tokens indefinitely.

Use full_auto for repository-scale work

level="full_auto" enables iterative mode, filesystem tracking, and sub-agent delegation — appropriate for code and data analysis over large corpora.

Combine with code execution for structured data

Pair ExecutionConfig(code_execution=True) with autonomy so agents can run Python over artifact contents rather than reasoning over raw text alone.


Related