Skip to main content

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

from praisonaiagents import ContextAgent, create_context_agent

Quick Example

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.
ParameterTypeDefaultDescription
namestr"Context Engineering Specialist"Agent name
rolestr"Expert Context Engineer..."Agent role
goalstr"Perform comprehensive codebase analysis..."Agent goal
backstorystrAuto-generatedAgent backstory
instructionsstrNoneCustom instructions
llmstrNoneLLM model to use
toolslistAuto-configuredTools for analysis
project_pathstrNonePath to project to analyze
auto_analyzeboolTrueAuto-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.
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.
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.
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.
validation = agent.create_validation_framework("/path/to/project")

Protocol

ContextAgent implements ContextEngineerProtocol:
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:
# 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):
# 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

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")