> ## 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.

# ContextAgent Module

> Advanced Context Engineering for AI coding assistants with PRP generation

# ContextAgent Module

The `ContextAgent` class implements advanced Context Engineering principles for AI coding assistants, following the PRD (Product Requirements Document) methodology.

## Key Features

* **10x better than prompt engineering**
* **100x better than vibe coding**
* Comprehensive context generation for first-try implementation success
* Systematic codebase analysis with modern tools
* PRP (Product Requirements Prompt) generation
* Validation loops and quality gates
* Saves every agent response for complete traceability

## Import

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

## Quick Example

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

# Create context agent
agent = create_context_agent(
    llm="gpt-4o-mini",
    name="Context Engineer"
)

# Analyze a codebase
analysis = agent.analyze_codebase_with_gitingest("/path/to/project")

# Generate PRP for a feature
prp = agent.generate_comprehensive_prp(
    feature_request="Add user authentication with OAuth2",
    context_analysis=analysis
)

print(prp)
```

## Constructor

### `ContextAgent()`

Creates a new ContextAgent instance.

| Parameter      | Type   | Default                                        | Description                |
| -------------- | ------ | ---------------------------------------------- | -------------------------- |
| `name`         | `str`  | `"Context Engineering Specialist"`             | Agent name                 |
| `role`         | `str`  | `"Expert Context Engineer..."`                 | Agent role                 |
| `goal`         | `str`  | `"Perform comprehensive codebase analysis..."` | Agent goal                 |
| `backstory`    | `str`  | Auto-generated                                 | Agent backstory            |
| `instructions` | `str`  | `None`                                         | Custom instructions        |
| `llm`          | `str`  | `None`                                         | LLM model to use           |
| `tools`        | `list` | Auto-configured                                | Tools for analysis         |
| `project_path` | `str`  | `None`                                         | Path to project to analyze |
| `auto_analyze` | `bool` | `True`                                         | Auto-analyze on init       |

## Phases

The ContextAgent follows a systematic 5-phase approach:

### Phase 1: Deep Codebase Analysis

Using gitingest, AST analysis, and other tools to understand the codebase structure.

### Phase 2: Pattern Extraction and Documentation

Identifying coding patterns, conventions, and architectural decisions.

### Phase 3: Comprehensive PRP Generation

Creating detailed Product Requirements Prompts for implementation.

### Phase 4: Validation Framework Creation

Building validation criteria and quality gates.

### Phase 5: Implementation Blueprint Generation

Generating step-by-step implementation guidance.

## Core Methods

### `analyze_codebase_with_gitingest(project_path)`

Analyzes a codebase using gitingest for comprehensive understanding.

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
analysis = agent.analyze_codebase_with_gitingest("/path/to/project")
print(analysis["project_structure"])
print(analysis["code_patterns"])
```

### `generate_comprehensive_prp(feature_request, context_analysis)`

Generates a comprehensive Product Requirements Prompt.

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
prp = agent.generate_comprehensive_prp(
    feature_request="Add real-time notifications",
    context_analysis=analysis
)
```

### `build_implementation_blueprint(feature_request, context_analysis)`

Creates a step-by-step implementation blueprint.

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
blueprint = agent.build_implementation_blueprint(
    feature_request="Add WebSocket support",
    context_analysis=analysis
)
for step in blueprint["implementation_steps"]:
    print(f"- {step}")
```

### `create_validation_framework(project_path)`

Creates validation criteria and quality gates.

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
validation = agent.create_validation_framework("/path/to/project")
```

## Protocol

ContextAgent implements `ContextEngineerProtocol`:

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

class ContextEngineerProtocol(Protocol):
    def analyze_codebase(self, project_path: str) -> Dict[str, Any]: ...
    def generate_prp(self, feature_request: str, context_analysis: Dict = None) -> str: ...
    def create_implementation_blueprint(self, feature_request: str, context_analysis: Dict = None) -> Dict: ...
    async def aanalyze_codebase(self, project_path: str) -> Dict[str, Any]: ...
    async def agenerate_prp(self, feature_request: str, context_analysis: Dict = None) -> str: ...
```

## Async Methods

All core methods have async versions for non-blocking execution:

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
# Async versions for non-blocking execution
analysis = await agent.aanalyze_codebase("/path/to/project")
prp = await agent.agenerate_prp("Feature description", analysis)
blueprint = await agent.acreate_implementation_blueprint("Feature", analysis)
```

## Configurable Output

Control output using the `output=` parameter (inherited from Agent):

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
# Silent mode - suppress all progress output
agent = create_context_agent(llm="gpt-4o-mini", output="silent")

# Verbose mode - show rich output with progress
agent = create_context_agent(llm="gpt-4o-mini", output="verbose")

# Default is silent (no output)
agent = create_context_agent(llm="gpt-4o-mini")
```

## Output Directory

Results are saved to `.praison/prp/` for complete traceability:

```
.praison/prp/
├── agent_responses/      # Individual agent responses
├── markdown_outputs/     # Formatted markdown reports
├── debug_logs/           # Debug logs (if enabled)
└── final_results/        # Final PRP and blueprints
```

## Example: Full Workflow

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

# Initialize agent
agent = create_context_agent(llm="gpt-4o-mini")

# Step 1: Analyze codebase
analysis = agent.analyze_codebase_with_gitingest("/path/to/my-app")
print(f"Found patterns: {analysis.get('code_patterns', {})}")

# Step 2: Extract implementation patterns
patterns = agent.extract_implementation_patterns("/path/to/my-app", analysis)

# Step 3: Generate PRP
prp = agent.generate_comprehensive_prp(
    "Add real-time notifications using WebSockets",
    context_analysis=analysis
)
print(prp)

# Step 4: Create implementation blueprint
blueprint = agent.build_implementation_blueprint(
    "Add real-time notifications",
    context_analysis=analysis
)

# Step 5: Create validation framework
validation = agent.create_validation_framework("/path/to/my-app")
```

## Related

* [Agent Module](/sdk/praisonaiagents/agent/agent) - Base Agent class
* [Context Management](/features/context-management) - Context window management
* [Knowledge Module](/sdk/praisonaiagents/knowledge/knowledge) - Knowledge base
