Skip to content

Decision Agent

Overview

The Decision Agent is responsible for processing and analyzing player choices, determining outcomes, and managing the game's decision-making logic using LLM-based analysis.

Core Architecture

graph TD
    subgraph Decision Agent
        DA[Decision Agent] --> DM[Decision Manager]
        DA --> AM[Analysis Module]
    end

    subgraph Components
        AM --> LA[LLM Analyzer]
        AM --> RC[Rules Checker]
        AM --> OD[Outcome Determiner]
    end

    DA --> |Validates| Rules[Rules Agent]
    DA --> |Updates| State[Game State]

Key Components

Decision Processing

  • Input Analysis

    • Player choice analysis
    • Context evaluation
    • Rule validation
  • Outcome Determination

    • Next section selection
    • Condition evaluation
    • State updates
  • LLM Integration

    • Semantic understanding
    • Context awareness
    • Response analysis
class DecisionAgent:
    async def analyze_response(
        self,
        section_number: int,
        user_response: str,
        rules: Dict
    ) -> AnalysisResult:
        messages = [
            SystemMessage(content=self.system_prompt),
            HumanMessage(content=f"""
                Section: {section_number}
                Response: {user_response}
                Rules: {json.dumps(rules, indent=2)}
            """)
        ]

        # LLM Analysis
        response = await self.llm.ainvoke(messages)

        # Parse and validate
        result = json.loads(response.content)
        return AnalysisResult(
            next_section=result["next_section"],
            conditions=result.get("conditions", []),
            analysis=result.get("analysis", "")
        )

Analysis System

The Decision Agent analyzes through multiple stages:

  1. Input Processing
  2. Player choice parsing
  3. Context extraction
  4. Rule gathering

  5. Decision Logic

  6. LLM-based analysis
  7. Rule validation
  8. Outcome determination

  9. Result Generation

  10. Next section selection
  11. Condition evaluation
  12. State update preparation

Decision Flow

sequenceDiagram
    participant P as Player
    participant DA as Decision Agent
    participant LLM as LLM Analyzer
    participant RA as Rules Agent
    participant ST as State

    P->>DA: Make Choice
    DA->>ST: Get Context
    DA->>RA: Get Rules
    DA->>LLM: Analyze Choice

    par Analysis
        LLM->>LLM: Process Context
        LLM->>LLM: Apply Rules
        LLM->>LLM: Determine Outcome
    end

    LLM-->>DA: Analysis Result
    DA->>RA: Validate Outcome
    DA->>ST: Update State
    DA-->>P: Response

Best Practices

  1. Analysis Design
  2. Clear context building
  3. Comprehensive rule checking
  4. Robust error handling

  5. LLM Integration

  6. Structured prompts
  7. Response validation
  8. Error recovery

  9. Performance

  10. Response caching
  11. Parallel validation
  12. State optimization

Error Handling

The Decision Agent implements robust error handling:

try:
    # Analyze decision
    result = await self._analyze_decision(context)

    # Validate with rules
    if await self.rules_agent.validate(result):
        return result
    raise DecisionError("Invalid decision outcome")

except DecisionError as e:
    logger.error("Decision error: {}", str(e))
    return DecisionResult(
        valid=False,
        error=str(e),
        fallback_section=context.current_section
    )

Performance Considerations

  1. LLM Optimization
  2. Prompt engineering
  3. Response caching
  4. Batch processing

  5. Validation Strategy

  6. Early validation
  7. Rule prioritization
  8. Cache management

  9. State Management

  10. Efficient updates
  11. Change tracking
  12. Memory optimization

Integration Points

  1. Rules Agent
  2. Rule validation
  3. Constraint checking
  4. Mechanics enforcement

  5. State Manager

  6. Context retrieval
  7. State updates
  8. History tracking

  9. Story Graph

  10. Flow control
  11. Section transitions
  12. Event processing