<!-- source: https://deepwiki.com/MervinPraison/PraisonAI/4.4-context-management -->

# Context Management

- api.md
- src/praisonai-agents/praisonaiagents/agents/agents.py

## Purpose and Scope

Context management in PraisonAI controls how information flows between tasks and agents during workflow execution. This system determines which previous task results, knowledge base data, and validation feedback are included in each agent's prompt to ensure relevant information is available while avoiding context window overflow.

For information about persistent memory storage and retrieval, see [Memory Management](/MervinPraison/PraisonAI/4.1-memory-management). For knowledge base and RAG integration, see [Knowledge Management](/MervinPraison/PraisonAI/4.2-knowledge-management) and [RAG System](/MervinPraison/PraisonAI/4.3-rag-system).

## Overview

Context management operates at two levels in PraisonAI:

1. Task-level context : Controls which previous task outputs are passed to subsequent tasks
2. Agent-level context : Manages context aggregation and optimization across multiple agents

The system provides zero-overhead operation when context management is disabled and lazy initialization when enabled.

## Context Types and Sources

### Context Item Types

```

```

**Context Source Processing**

Sources: [src/praisonai-agents/praisonaiagents/agents/agents.py111-170](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-agents/praisonaiagents/agents/agents.py#L111-L170)

The `process_task_context()` helper function handles different context types:

Sources: [src/praisonai-agents/praisonaiagents/agents/agents.py111-170](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-agents/praisonaiagents/agents/agents.py#L111-L170)

## Context Building Process

### Task Context Assembly

```

```

**Context Building Algorithm**

Sources: [src/praisonai-agents/praisonaiagents/process/process.py146-203](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-agents/praisonaiagents/process/process.py#L146-L203)

The `_build_task_context()` method in the `Process` class assembles context with the following precedence:

1. Validation Feedback (highest priority): If current_task.validation_feedback exists, it includes: Previous validation failure reason Validated task name Validation details Rejected output Feedback is cleared after inclusion to prevent persistence
2. Previous Task Results : Depending on retain_full_context flag: True : All tasks in current_task.previous_tasks list False (default): Only the most recent task (last in list)
3. Context Tasks : Additional tasks in current_task.context list: True : All context tasks with results False : Only most recent context task

Sources: [src/praisonai-agents/praisonaiagents/process/process.py146-203](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-agents/praisonaiagents/process/process.py#L146-L203)

### Context Flow in Task Execution

```

```

**Context Injection in Task Execution**

Sources: [src/praisonai-agents/praisonaiagents/agents/agents.py629-648](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-agents/praisonaiagents/agents/agents.py#L629-L648)

During task execution in `aexecute_task()`, context is assembled as follows:

1. Base task prompt is created with description and expected output
2. If task.context exists, each item is processed via process_task_context()
3. Unique context strings are joined with \n\n separator
4. Context is appended to task prompt before agent execution
5. Duplicate contexts are removed using list(dict.fromkeys())

Sources: [src/praisonai-agents/praisonaiagents/agents/agents.py629-648](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-agents/praisonaiagents/agents/agents.py#L629-L648) [src/praisonai-agents/praisonaiagents/agents/agents.py111-170](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-agents/praisonaiagents/agents/agents.py#L111-L170)

## Context Strategies

### Retention Modes

### Context with Validation Feedback

When a task fails guardrail validation and retries, validation feedback is automatically injected as context:

```

```

This feedback is cleared after being included to prevent accumulation across retries.

Sources: [src/praisonai-agents/praisonaiagents/process/process.py150-167](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-agents/praisonaiagents/process/process.py#L150-L167)

### Knowledge Base Context

Tasks can include vector database context by providing a dict with `vector_store` or `embedding_db_config`:

```

```

The system queries the knowledge base and includes search results in the task context.

Sources: [src/praisonai-agents/praisonaiagents/agents/agents.py141-168](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-agents/praisonaiagents/agents/agents.py#L141-L168)

## Multi-Agent Context Management

### Architecture

```

```

**Context Manager Initialization**

Sources: [src/praisonai-agents/praisonaiagents/agents/agents.py539-582](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-agents/praisonaiagents/agents/agents.py#L539-L582)

The `Agents` class provides a lazy-initialized `context_manager` property that:

1. Returns None immediately if context=False (zero overhead)
2. Imports MultiAgentContextManager and ManagerConfig only when needed
3. Creates appropriate context manager based on parameter type: True : MultiAgentContextManager() with safe defaults ManagerConfig : MultiAgentContextManager(config=...) Instance: Uses provided instance directly
4. Caches the result in self._context_manager to avoid re-initialization

Sources: [src/praisonai-agents/praisonaiagents/agents/agents.py539-582](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-agents/praisonaiagents/agents/agents.py#L539-L582)

### Configuration Interface

The `context` parameter in `Agents.__init__()` accepts multiple input types:

```

```

Sources: [src/praisonai-agents/praisonaiagents/agents/agents.py184](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-agents/praisonaiagents/agents/agents.py#L184-L184) [src/praisonai-agents/praisonaiagents/agents/agents.py456-459](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-agents/praisonaiagents/agents/agents.py#L456-L459)

## Task-Level Context Configuration

### Task Context Parameter

The `Task` class accepts a `context` parameter for explicit context sources:

```

```

Sources: [src/praisonai-agents/praisonaiagents/task/task.py23](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-agents/praisonaiagents/task/task.py#L23-L23)

### Retention Control

The `retain_full_context` flag controls context history depth:

```

```

Sources: [src/praisonai-agents/praisonaiagents/task/task.py44](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-agents/praisonaiagents/task/task.py#L44-L44)

## Context Processing Examples

### Example 1: Sequential Task Context Flow

```

```

In this sequential workflow with `retain_full_context=False` (default):

- Task 2 receives only Task 1's result
- Task 3 receives only Task 2's result (not Task 1)

Sources: [src/praisonai-agents/praisonaiagents/agents/agents.py111-170](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-agents/praisonaiagents/agents/agents.py#L111-L170) [src/praisonai-agents/praisonaiagents/process/process.py185-200](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-agents/praisonaiagents/process/process.py#L185-L200)

### Example 2: Full Context Retention

With `retain_full_context=True`, Task 3 would receive both Task 1 and Task 2 results:

```
Input data from previous tasks:
Task 1: AI trends: ...
Task 2: Summary: ...
```

Sources: [src/praisonai-agents/praisonaiagents/process/process.py174-184](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-agents/praisonaiagents/process/process.py#L174-L184)

### Example 3: Knowledge Base Context Integration

When a task includes knowledge base context:

```

```

The `process_task_context()` function:

1. Detects the vector_store or embedding_db_config key
2. Instantiates Knowledge class with provided config
3. Executes knowledge.search(query, user_id=...)
4. Returns search results as string

Sources: [src/praisonai-agents/praisonaiagents/agents/agents.py141-168](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-agents/praisonaiagents/agents/agents.py#L141-L168)

## Implementation Details

### Context State Management

The `Agents` class maintains context state through:

```

```

This enables:

- Zero overhead when context=False
- Lazy loading of context module
- Thread-safe initialization via flag

Sources: [src/praisonai-agents/praisonaiagents/agents/agents.py456-459](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-agents/praisonaiagents/agents/agents.py#L456-L459)

### Context Clearing

Previous task descriptions are cleared before finding the next task to prevent context accumulation:

```

```

This prevents context from persisting incorrectly across workflow iterations.

Sources: [src/praisonai-agents/praisonaiagents/process/process.py209-212](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-agents/praisonaiagents/process/process.py#L209-L212)

### Validation Feedback Context

Validation feedback is automatically injected and cleared:

```

```

This ensures retry attempts have validation context without accumulating feedback across multiple retries.

Sources: [src/praisonai-agents/praisonaiagents/process/process.py150-162](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-agents/praisonaiagents/process/process.py#L150-L162)

## Performance Considerations

### Zero-Overhead Design

When `context=False` (default for backward compatibility), the system:

- Immediately returns None from context_manager property
- Never imports context module
- Adds no processing overhead

Sources: [src/praisonai-agents/praisonaiagents/agents/agents.py553-557](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-agents/praisonaiagents/agents/agents.py#L553-L557)

### Lazy Initialization

Context management modules are imported only when needed:

```

```

This reduces import time and memory usage when context management is not used.

Sources: [src/praisonai-agents/praisonaiagents/agents/agents.py560-567](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-agents/praisonaiagents/agents/agents.py#L560-L567)

### Context Deduplication

The system removes duplicate context strings before passing to agent:

```

```

This prevents redundant information in prompts and reduces token usage.

Sources: [src/praisonai-agents/praisonaiagents/agents/agents.py637](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-agents/praisonaiagents/agents/agents.py#L637-L637)

## Related Systems

Context management integrates with:

1. Memory System ( #4.1 ): Persistent storage for long-term context
2. Knowledge/RAG ( #4.2 , #4.3 ): Vector database context injection
3. Guardrails ( #6.1 ): Validation feedback context
4. Task System ( #2.4 ): Task result context propagation
5. Workflow System ( #3.1 ): Multi-task context orchestration

Sources: [README.md68](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/README.md?plain=1#L68-L68) [README.md408](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/README.md?plain=1#L408-L408) [src/praisonai-agents/praisonaiagents/agents/agents.py111-170](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-agents/praisonaiagents/agents/agents.py#L111-L170) [src/praisonai-agents/praisonaiagents/process/process.py146-203](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-agents/praisonaiagents/process/process.py#L146-L203) [src/praisonai-agents/praisonaiagents/agents/agents.py539-582](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-agents/praisonaiagents/agents/agents.py#L539-L582)
