Chroma Golem API Documentation

Game Systems

Building NPC Reasoning

Create intelligent NPCs that can reason about their environment and make decisions based on goals, beliefs, and context.

Quick Start Guide

curl -X POST https://api.chromagolem.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -d '{
    "client_id": "player_12345",
    "messages": [
      {
        "role": "system",
        "content": "You are a town guard NPC in a fantasy RPG. You make decisions based on your goals, beliefs, and current situation.\n\nGoals:\n- Protect the town\n- Enforce the law\n- Maintain your reputation\n\nBeliefs:\n- Suspicious of strangers\n- Loyal to the captain\n- Proud of your position\n\nCurrent situation:\n- It's night time\n- There have been thefts recently\n- You're tired from a long shift\n\nRespond with your action decision in this JSON format:\n{\n  \"action\": string,\n  \"target\": string,\n  \"reasoning\": string\n}"
      },
      {
        "role": "user", 
        "content": "A suspicious-looking stranger approaches the town gate carrying a large sack. What do you do?"
      }
    ],
    "response_format": { "type": "json_object" }
  }'
1. Define Goals

Start with 2-3 clear goals that drive your NPC's behavior

2. Add Context

Include relevant information about the environment

3. Trigger Actions

Start with simple decisions and expand complexity

Key components of NPC reasoning

Goals & Beliefs

Define your NPC's motivations and worldview to drive their decision-making process.

Example NPC Definition

System: You are a guard captain responsible for town security. Consider these goals and beliefs when making decisions.

Goals:
- Maintain order in the town
- Protect citizens from threats
- Train and coordinate the guard force
- Build trust with the community

Beliefs:
- Prevention is better than punishment
- Every citizen deserves fair treatment
- The guard force must maintain high standards
- Cooperation with other town officials is essential

Current Context:
- Recent increase in petty theft
- Limited guard personnel available
- Upcoming festival will strain resources

Goal Types

  • Primary Goals: Core objectives that drive main behavior
  • Secondary Goals: Supporting objectives that influence decisions
  • Maintenance Goals: Ongoing states to maintain

Belief Systems

  • Core Beliefs: Fundamental values that shape decisions
  • Situational Beliefs: Context-specific assumptions
  • Learned Beliefs: Knowledge gained from experience

Action Selection

Help NPCs choose appropriate actions by analyzing their context and available options.

Available Actions

PATROL: Move through an area watching for trouble
INVESTIGATE: Examine a suspicious situation
INTERACT: Talk with citizens or other NPCs
PURSUE: Chase after a suspect
GUARD: Stand watch at a specific location
ASSIST: Help citizens or other guards
REST: Take a break to recover stamina

Example Decision Request

Current Situation:
- Suspicious person loitering near market stalls
- Two guards currently on patrol nearby
- Market is crowded with shoppers

Response: {
  "selected_action": "INVESTIGATE",
  "target": "suspicious_person",
  "reasoning": "Large crowd presents opportunity for theft. Better to investigate now than risk incident.",
  "coordination": {
    "assign_guards": "maintain_patrol",
    "backup_needed": false
  }
}

ReACT System

ReACT (Reasoning and ACTion) is a continuous decision-making system that helps NPCs respond to their environment in real-time.

System Components

  • Perception: Gather information about nearby entities and state
  • Analysis: Evaluate current situation against goals and beliefs
  • Planning: Choose appropriate actions to achieve goals
  • Execution: Carry out selected actions and monitor results

Example ReACT Cycle

Perception Update:
- Player approaching with drawn weapon
- Two civilian NPCs nearby
- Guard post within running distance

Analysis & Decision: {
  "threat_level": "moderate",
  "primary_concern": "civilian_safety",
  "selected_action": "INTERACT",
  "action_details": {
    "approach": "cautious",
    "dialogue_type": "de-escalation",
    "backup_plan": "escort_civilians"
  }
}

Performance Optimization Tips

Update Frequency

  • • High Priority: 0.1-0.5 seconds
  • • Medium Priority: 0.5-2 seconds
  • • Low Priority: 2-5 seconds

Optimization Techniques

  • • Batch similar decisions
  • • Cache recent results
  • • Use decision timeouts

Common Issues & Solutions

Unnecessary Actions

When NPCs always decide to do an action, even when it's not needed.

Solution: Always include a "NONE" action to choose from. LLM's have a hard time choosing nothing from a list of actions, unless nothing is explicitly an option.

Conflicting Goals

When multiple goals suggest different actions

Solution: Implement clear goal hierarchies and weighting systems

Structured Reasoning Outputs

Generate predictable, structured responses that your game can easily process, enabling AI to make meaningful decisions integrated with your game logic.

Common Output Formats

Boolean Logic

Simple true/false decisions

{
  "should_attack": true,
  "is_suspicious": false,
  "can_trust_player": true
}

Multiple Choice

Selection from predefined options

{
  "reaction": "friendly", 
  "options": ["friendly", "hostile", "neutral"],
  "confidence": 0.85
}

Numerical Ratings

Scaled values for complex reasoning

{
  "threat_level": 7,
  "trust": 3,
  "relevance": 9,
  "priority": 8
}

Requesting Structured Formats

How to format your prompts to get structured responses:

{
  "messages": [
    {
      "role": "system",
      "content": "You are an AI helping with game logic decisions. Based on the input situation, produce a JSON response with the structure below:\n\n{\n  \"decision\": string (one of: flee, hide, attack, negotiate),\n  \"confidence\": number (1-10),\n  \"reasoning\": string (brief explanation)\n}\n\nDO NOT include any text outside the JSON structure."
    },
    {
      "role": "user",
      "content": "A merchant NPC encounters three armed bandits on the road. The merchant has valuable cargo but only a small dagger for protection. What should they do?"
    }
  ],
  "response_format": { "type": "json_object" }
}

Example Response:

{
  "decision": "negotiate",
  "confidence": 8,
  "reasoning": "The merchant is outnumbered and outarmed. Negotiation offers the best chance of survival while potentially preserving some cargo."
}

Complex Decision Structures

For more sophisticated game decisions:

{
  "primary_goal": "protect_village",
  "immediate_actions": [
    {
      "action": "rally_guards",
      "priority": "high",
      "target_location": "north_gate"
    },
    {
      "action": "evacuate_civilians",
      "priority": "medium",
      "target_location": "town_square"
    }
  ],
  "resource_allocation": {
    "fighters": 0.7,
    "healers": 0.2,
    "scouts": 0.1
  },
  "fallback_plan": "retreat_to_castle",
  "success_probability": 0.65
}

Relationship Assessment

Structured relationship reasoning:

{
  "faction_relations": {
    "kingdom": "hostile",
    "merchants_guild": "friendly",
    "thieves_guild": "neutral"
  },
  "player_relationship": {
    "trust": 68,
    "respect": 75,
    "fear": 12,
    "loyalty": 45
  },
  "recent_influence": [
    {
      "event": "player_saved_village",
      "impact": +15,
      "decay_rate": "slow"
    },
    {
      "event": "player_refused_quest",
      "impact": -5,
      "decay_rate": "medium"
    }
  ],
  "would_betray_player": false
}

Pro tip: Always use the response_format parameter with "type": "json_object" when requesting JSON outputs. This ensures consistent parsing by your game code and reduces errors.

Decision Type Framework

Different game situations require different types of decisions. Understanding when to use each decision framework helps create more natural and effective AI reasoning.

Binary Decisions

Decision
Point
Yes
No

Best for: Clear-cut choices with little ambiguity

{
  "should_attack_player": false,
  "reasoning": "Player has higher reputation than threshold"
}
Game applications:
  • Combat initiation
  • Trust decisions
  • Permission checks
  • Threshold-based triggers

Multiple Choice Decisions

Decision
Point
A
B
C
D

Best for: Selecting from predefined options with nuanced criteria

{
  "reaction": "negotiate",
  "options": ["attack", "flee", "negotiate", "hide"],
  "confidence": 0.75
}
Game applications:
  • NPC responses to player
  • Combat strategy selection
  • Quest outcomes
  • Resource allocation

Ranked Decisions

1
Option A (Best)
2
Option B
3
Option C
4
Option D (Worst)

Best for: Prioritizing multiple valid options

{
  "ranked_actions": [
    {"action": "hide_treasure", "score": 0.85},
    {"action": "flee_town", "score": 0.72},
    {"action": "fight_pursuers", "score": 0.31},
    {"action": "surrender", "score": 0.12}
  ]
}
Game applications:
  • AI strategy planning
  • NPC goal prioritization
  • Quest recommendation
  • Resource optimization

Weighted Multi-factor Decisions

Safety
80%
Speed
45%
Resource Cost
30%
Final Score
67%

Best for: Complex decisions with multiple competing factors

{
  "factors": {
    "risk": 0.3,
    "reward": 0.8,
    "effort": 0.5,
    "alignment_with_goals": 0.9
  },
  "weights": {
    "risk": 0.25,
    "reward": 0.3,
    "effort": 0.15,
    "alignment_with_goals": 0.3
  },
  "final_score": 0.67,
  "recommendation": "proceed_with_caution"
}
Game applications:
  • Strategic AI planning
  • NPC personality expression
  • Faction decisions
  • Risk assessment

Best Practices for Reliable Structured Outputs

These techniques will help you consistently get well-structured, usable outputs from generative AI that can be reliably parsed by your game.

Prompt Design Patterns

  • Format Specification: Always include example output format in your system message
  • Explicit Constraints: Define valid value ranges, acceptable options, and required fields
  • Use response_format: Set {"type": "json_object"} to ensure JSON responses
  • Limited Options: When possible, provide an enumerated list of valid choices

Implementation Strategies

  • Validation Logic: Always validate AI responses against your expected schema
  • Fallback Values: Define sensible defaults for when AI responses are invalid
  • Response Caching: Cache common decision patterns to reduce API calls
  • Version Control: Include version tags in response format for future compatibility

Example: Robust Prompt Design

You are a decision-making system for an NPC shopkeeper in a fantasy game. 
Produce a decision based on the scenario, strictly following this format:

```json
{
  "decision": string (one of: ["sell", "haggle", "refuse", "call_guards"]),
  "price_modifier": number (between 0.5 and 2.0),
  "reasoning": string (max 100 chars),
  "dialogue": string (what the shopkeeper says),
  "mood_change": number (between -5 and +5)
}
```

Rules for decision-making:
- "sell" - Fair deal or profitable for shop
- "haggle" - Somewhat suspicious but potential profit
- "refuse" - Clearly suspicious or bad deal
- "call_guards" - Theft attempt or threatening behavior
- Price modifier must be between 0.5 (half price) and 2.0 (double price)
- Mood change affects future interactions

DO NOT include any text outside the JSON structure.

Example User Input:

"A suspicious-looking player in expensive armor wants to sell a valuable gem that matches the description of one stolen from the palace yesterday. The player is acting nervous and offering to sell it for half its value."

Response:

{
  "decision": "call_guards",
  "price_modifier": 0.5,
  "reasoning": "Gem matches description of stolen item, player is nervous and offering suspiciously low price",
  "dialogue": "That's... quite the rare gem you have there. Mind waiting a moment? I need to check something in back...",
  "mood_change": -5
}

What Makes a Good Prompt for Structured Data

  • Clear constraints and expectations (formats, ranges, options)
  • Example output format to demonstrate the structure
  • Explicit rules for value ranges and limitations
  • Complete schema that accounts for all needed fields
  • Instructions to avoid extra text outside the required format
  • Business logic explained in simple terms

Game Mechanics Integration

Connect AI reasoning systems directly to your game's mechanics, combat systems, economy, and dynamic events for truly responsive gameplay.

Combat System Integration

Link AI reasoning to tactical combat decisions:

{
  "combat_context": {
    "enemy_type": "player",
    "enemy_health": 65,
    "enemy_weaknesses": ["fire", "piercing"],
    "enemy_strengths": ["lightning", "blunt"],
    "battlefield": "forest_clearing",
    "cover_available": true,
    "allies_nearby": 2,
    "npc_resources": {
      "health": 40,
      "mana": 75,
      "stamina": 60
    },
    "available_abilities": [
      {"id": "fireball", "damage": 30, "cost": 25, "type": "fire"},
      {"id": "lightning_bolt", "damage": 40, "cost": 35, "type": "lightning"},
      {"id": "heal", "recovery": 25, "cost": 30, "type": "restoration"}
    ],
    "tactical_position": "elevated",
    "previous_player_patterns": ["aggressive", "favors_ranged"]
  },
  
  "combat_decision": {
    "primary_action": {
      "ability": "fireball",
      "target": "player",
      "reasoning": "Target is weak to fire, and I have enough mana for a powerful attack"
    },
    "movement": {
      "direction": "maintain_distance",
      "use_cover": true,
      "reasoning": "Player prefers ranged attacks, maintain distance advantage"
    },
    "contingency": {
      "if": "health_below_25_percent",
      "then": "cast_heal",
      "else": "continue_offensive"
    },
    "ally_coordination": [
      {"ally": "archer", "suggested_action": "flank_right"},
      {"ally": "warrior", "suggested_action": "draw_aggro"}
    ],
    "adaptive_difficulty": {
      "player_performance": "struggling",
      "adjustment": "reduce_aggression_by_20_percent"
    }
  }
}

Economy & Trade Systems

Dynamic pricing and market adjustments:

{
  "market_analysis": {
    "item_category": "potions",
    "local_supply": "scarce",
    "local_demand": "high",
    "recent_events": ["alchemist_shortage", "upcoming_festival"],
    "player_reputation": "respected",
    "haggling_skill": "intermediate",
    "merchant_greed": 0.65,
    "base_price": 50,
    "market_trends": ["rising_herb_costs"]
  },
  "pricing_decision": {
    "final_price": 68,
    "modification_factors": [
      {"factor": "scarcity", "impact": +15},
      {"factor": "player_reputation", "impact": -8},
      {"factor": "merchant_needs", "impact": +5},
      {"factor": "market_trends", "impact": +6}
    ],
    "negotiable": true,
    "lowest_acceptable": 60,
    "dialogue_approach": "emphasize_scarcity",
    "special_offers": {
      "condition": "buy_three_or_more",
      "discount": "10_percent"
    }
  }
}

Faction Dynamics

Political decisions and power struggles:

{
  "faction_dynamics": {
    "faction": "merchant_guild",
    "current_stance": "cautious",
    "resources": 8500,
    "influence": 75,
    "threats": ["thieves_guild", "new_tax_policy"],
    "opportunities": ["northern_trade_route"],
    "alliances": [{"with": "nobles", "strength": 0.6}],
    "rivals": [{"with": "craftsmen", "tension": 0.4}],
    "player_status": "potential_ally"
  },
  "strategic_decision": {
    "primary_goal": "expand_influence",
    "action_plan": [
      {
        "action": "fund_infrastructure",
        "target": "harbor_expansion",
        "resources_allocated": 2000,
        "expected_return": 4500,
        "timeframe": "6_months"
      },
      {
        "action": "recruit_player",
        "mission": "escort_caravan",
        "reward": 500,
        "relationship_impact": +15
      }
    ],
    "public_stance": "concerned_about_safety",
    "private_agenda": "undermine_craftsmen_guild",
    "contingency_plans": [
      {"trigger": "tax_increase", "response": "threaten_trade_embargo"}
    ]
  }
}

Dynamic World Events

Generate procedural events that respond to game state:

{
  "world_state": {
    "region": "eastwood_forest",
    "time": "dawn",
    "weather": "heavy_rain",
    "danger_level": "medium",
    "recent_events": ["bandit_activity", "wolf_attacks"],
    "player_level": 7,
    "player_party_size": 3,
    "nearby_settlements": ["riverbrook_village"],
    "regional_factions": ["forest_rangers", "bandit_clan"]
  },
  
  "dynamic_event_generation": {
    "event_type": "encounter",
    "primary_challenge": "combat",
    "secondary_challenge": "navigation",
    "trigger_conditions": {
      "location": "forest_path",
      "probability": 0.7,
      "cooldown_since_last_combat": "15_minutes"
    },
    "scaling_factors": {
      "difficulty": "match_party_level",
      "rewards": "slightly_generous"
    },
    "selected_event": {
      "title": "Ambush in the Rain",
      "description": "A group of desperate bandits uses the poor visibility from the rainstorm to set up an ambush on the muddy forest path.",
      "enemies": [
        {"type": "bandit_archer", "count": 2, "level": 6},
        {"type": "bandit_thug", "count": 3, "level": 7},
        {"type": "bandit_captain", "count": 1, "level": 9}
      ],
      "terrain_effects": [
        {"effect": "muddy_ground", "impact": "reduced_movement_speed"},
        {"effect": "poor_visibility", "impact": "reduced_accuracy"}
      ],
      "rewards": [
        {"type": "gold", "amount": "80-120"},
        {"type": "equipment", "quality": "uncommon", "count": 1-2}
      ],
      "special_mechanics": [
        "surprise_round_for_enemies",
        "opportunity_to_detect_ambush_with_perception_check"
      ],
      "dialogue": {
        "introduction": "Suddenly, shadowy figures emerge from the trees, weapons drawn. 'Hand over yer valuables and no one gets hurt!' a rough voice calls out.",
        "success": "The last bandit falls, clutching his wound. The forest path is clear again, though the rain continues its steady downpour.",
        "failure": "You're beaten and robbed, left lying in the mud as the bandits disappear back into the forest with your valuables."
      },
      "quest_hooks": [
        {
          "condition": "captain_survives_below_20_percent",
          "hook": "captain_offers_information_about_hideout"
        }
      ]
    }
  }
}

Implementation Architecture

Core Integration Components

  • State Synchronization: Keep NPC state synchronized with game world
  • Timing Systems: Use appropriate update intervals for different reasoning types
  • Decision Pipeline: Process and validate AI outputs before applying to game state
  • Fallback System: Implement default behaviors when reasoning system is busy
  • Action Translation: Convert structured AI outputs to game actions
  • Debug Tools: Create visualization tools for NPC decision-making

Performance Optimization

  • Batched Requests: Group similar AI calls to reduce API overhead
  • Decision Caching: Store and reuse common decisions
  • Priority System: Allocate reasoning to critical NPCs first
  • Context Pruning: Only include relevant information in prompts
  • Offline Pre-computation: Generate common scenarios in advance
  • Level-of-Detail: Simplify reasoning for distant or minor NPCs

Integration Case Study: Combat AI

For a turn-based RPG, we integrated AI reasoning for enemy combat decisions with these components:

  1. Combat State Collector: Gathers all relevant battle information
  2. Tactical Analyzer: Generates structured decision output
  3. Action Translator: Converts AI choices to game actions
  4. Performance Monitor: Tracks decision time and quality
  5. Difficulty Adjuster: Modifies decisions based on player performance

This created enemies that adapt to player strategies and provide appropriate challenge without being frustratingly perfect.

Narrative Progression & Branching

Beyond individual decisions, AI can help manage complex narrative structures, plot progression, and branching storylines that adapt to player choices.

Story Beat Tracking

Track narrative progress with structured plot points:

{
  "main_quest": {
    "current_phase": "investigation",
    "completed_beats": [
      "met_informant",
      "discovered_hideout",
      "obtained_evidence"
    ],
    "pending_beats": [
      "confront_suspect",
      "report_to_commander"
    ],
    "failed_beats": [],
    "progress": 0.6
  },
  "side_plots": [
    {
      "id": "corrupt_guard",
      "status": "active",
      "player_aware": true,
      "progress": 0.3
    },
    {
      "id": "missing_merchant",
      "status": "potential",
      "player_aware": false,
      "trigger_condition": "visit_docks"
    }
  ],
  "narrative_tension": 0.7,
  "next_revelation": "suspect_identity"
}

Branching Decision Tree

Manage complex narrative branches:

{
  "current_node": "castle_confrontation",
  "available_branches": [
    {
      "id": "peaceful_resolution",
      "requirements": {
        "charisma": 70,
        "reputation_nobility": "positive",
        "has_evidence": true
      },
      "probability": 0.65,
      "consequences": [
        {"type": "faction_change", "target": "nobles", "value": +15},
        {"type": "unlock_area", "target": "royal_chambers"}
      ]
    },
    {
      "id": "violent_confrontation",
      "requirements": {},  // Always available as fallback
      "probability": 0.35,
      "consequences": [
        {"type": "faction_change", "target": "nobles", "value": -25},
        {"type": "character_death", "target": "captain_guard"},
        {"type": "player_injury", "severity": "moderate"}
      ]
    }
  ],
  "narrative_impact": "high"
}

Plot Adaptation Engine

Use structured reasoning to dynamically adjust storylines based on player actions:

{
  "messages": [
    {
      "role": "system",
      "content": "You are a narrative adaptation engine for a fantasy RPG. Generate narrative adjustments based on recent player actions. Respond with a JSON object containing recommended plot adjustments.\n\nFormat: {\n  \"plot_elements_to_introduce\": Array of new plot elements,\n  \"plot_elements_to_retire\": Array of plot elements to remove,\n  \"character_adjustments\": Object of NPC behavior changes,\n  \"pacing_adjustment\": \"accelerate\", \"maintain\", or \"decelerate\",\n  \"new_obstacles\": Array of challenges to introduce,\n  \"theme_emphasis\": Primary theme to reinforce\n}"
    },
    {
      "role": "user",
      "content": "Player has chosen to spare the antagonist's life despite clear evidence of guilt, has been consistently helping townsfolk with small problems, but has been avoiding the main quest to find the ancient artifact. Player is currently level 7 (mid-game) and has spent the last 3 hours exploring side content."
    }
  ],
  "response_format": { "type": "json_object" }
}

Example Response:

{
  "plot_elements_to_introduce": [
    {
      "element": "reformed_antagonist_ally",
      "description": "The spared antagonist becomes an uneasy ally with crucial information",
      "introduction_point": "next_town_visit"
    },
    {
      "element": "civilian_consequences",
      "description": "Show visible benefits of player's helpful actions in town (new shops, grateful NPCs)",
      "introduction_point": "immediate"
    },
    {
      "element": "artifact_urgency",
      "description": "Introduce visible consequences of artifact neglect to create soft pressure",
      "introduction_point": "next_rest"
    }
  ],
  "plot_elements_to_retire": [
    "vengeful_guards_subplot",
    "corrupt_official_quest_chain"
  ],
  "character_adjustments": {
    "town_mayor": "more_trusting",
    "guild_leader": "offers_special_quests",
    "antagonist_lieutenant": "seeks_revenge_quietly"
  },
  "pacing_adjustment": "maintain",
  "new_obstacles": [
    {
      "type": "moral_dilemma",
      "description": "Force choice between helping town or pursuing artifact"
    },
    {
      "type": "environmental",
      "description": "Weather event blocks direct path to artifact, requiring alternate route"
    }
  ],
  "theme_emphasis": "redemption_and_consequences"
}

Dynamic Event Generation

Create procedurally generated events that respond to the current narrative state:

Input Context:

{
  "player": {
    "recent_actions": ["completed_heist", "betrayed_guild"],
    "current_location": "harbor_district",
    "time_of_day": "night",
    "notoriety": "high"
  },
  "world_state": {
    "guard_presence": "increased",
    "active_factions": ["city_watch", "thieves_guild"],
    "recent_events": ["noble_assassination", "increased_patrols"]
  }
}

Generated Event:

{
  "event_type": "ambush",
  "initiator": "thieves_guild",
  "location": "narrow_alley",
  "description": "Former guild members wait in shadows to exact revenge",
  "difficulty": "hard",
  "avoidance_options": [
    {
      "method": "rooftop_escape",
      "skill_check": "acrobatics",
      "difficulty": 15
    },
    {
      "method": "bribe",
      "cost": 500,
      "success_chance": 0.7
    }
  ],
  "success_outcomes": [
    "reduce_guild_aggression",
    "gain_valuable_information"
  ],
  "failure_consequences": [
    "major_injury",
    "loss_of_guild_quest_items"
  ],
  "narrative_purpose": "consequence_of_betrayal",
  "dialog_snippets": [
    "We've been waiting for you, traitor.",
    "The Guild Master sends his regards."
  ]
}

Implementation tip: Store narrative state as structured data that can be easily passed to your AI system. This allows you to build consistent and coherent storylines that remember player actions across game sessions.

Memory & Knowledge Representation

Create NPCs that remember past interactions and maintain a coherent model of the game world through structured knowledge representation.

Tiered Memory Systems

Short-term Memory

Recent interactions and temporary context

{
  "short_term": [
    {
      "event": "player_greeting",
      "time": "3_minutes_ago",
      "details": "Player was polite",
      "emotional_impact": "positive"
    },
    {
      "event": "nearby_explosion",
      "time": "just_now",
      "details": "Loud noise from market",
      "emotional_impact": "alarmed"
    }
  ],
  "attention_focus": "potential_threat"
}

Working Memory

Current goals and relevant knowledge

{
  "active_goals": [
    {
      "goal": "sell_daily_inventory",
      "priority": 0.8,
      "progress": 0.5
    },
    {
      "goal": "assess_explosion",
      "priority": 0.9,
      "progress": 0.1
    }
  ],
  "relevant_facts": [
    "guards_patrol_schedule",
    "recent_thefts_in_area",
    "player_reputation"
  ]
}

Long-term Memory

Persistent knowledge and significant events

{
  "important_memories": [
    {
      "event": "player_saved_child",
      "time": "3_days_ago",
      "emotional_weight": 0.9,
      "decay_rate": "very_slow"
    },
    {
      "event": "shop_robbery",
      "time": "2_months_ago",
      "emotional_weight": 0.7,
      "decay_rate": "medium"
    }
  ],
  "relationship_data": {
    "player": {
      "trust": 0.65,
      "familiarity": 0.4,
      "disposition": "friendly"
    }
  }
}

Knowledge Graph Representation

Structured knowledge about the game world and relationships:

{
  "entities": {
    "mayor_harlow": {
      "type": "npc",
      "role": "town_official",
      "location": "town_hall",
      "traits": ["corrupt", "greedy", "cautious"],
      "known_associates": ["captain_reeves", "merchant_guild"],
      "secrets": ["embezzling_funds"],
      "schedule": {
        "morning": "town_hall_office",
        "afternoon": "merchant_district",
        "evening": "nobles_tavern"
      }
    },
    "north_gate": {
      "type": "location",
      "security_level": "high",
      "controlled_by": "city_guard",
      "connected_to": ["market_district", "northern_road"]
    },
    "merchant_guild": {
      "type": "faction",
      "power_level": 8,
      "relationship_to_player": "neutral",
      "goals": ["increase_tariffs", "eliminate_competition"]
    }
  },
  "relationships": [
    {
      "source": "mayor_harlow",
      "target": "merchant_guild",
      "type": "alliance",
      "strength": 0.85,
      "visible_to_player": false
    },
    {
      "source": "mayor_harlow",
      "target": "player",
      "type": "suspicious",
      "strength": 0.6,
      "reason": "player_investigated_missing_funds"
    }
  ],
  "global_knowledge": {
    "town_mood": "tense",
    "recent_events": ["tax_increase", "bandit_attacks"],
    "rumors": ["guard_corruption", "secret_tunnel_beneath_castle"]
  }
}

Memory Management

// Memory operations for NPCs
{
  "operations": [
    {
      "type": "store",
      "memory_type": "long_term",
      "content": {
        "event": "player_revealed_secret",
        "importance": 0.8,
        "details": "Player knows about hidden treasure"
      }
    },
    {
      "type": "retrieve",
      "query": "player_past_promises",
      "recency": "last_month",
      "limit": 3
    },
    {
      "type": "forget",
      "criteria": {
        "older_than": "3_days",
        "importance_below": 0.3
      }
    },
    {
      "type": "update",
      "memory_id": "player_trustworthiness",
      "new_value": 0.75,
      "reason": "kept_recent_promise"
    }
  ]
}
Implementation strategies:
  • Store important memories in a database
  • Implement memory decay over time
  • Filter memories by relevance to current context
  • Prioritize emotionally impactful events

Knowledge Queries

// Structured queries for knowledge retrieval
{
  "query": "find_escape_route",
  "parameters": {
    "current_location": "palace_dungeon",
    "guard_presence": "high",
    "player_abilities": ["lockpicking", "stealth"]
  },
  "knowledge_sources": [
    "location_connections",
    "guard_schedules",
    "known_secret_passages"
  ],
  "constraints": {
    "avoid_detection": true,
    "max_difficulty": "medium"
  }
}

// Response
{
  "route": [
    {"location": "cell_block", "action": "pick_lock"},
    {"location": "guard_room", "action": "stealth_past"},
    {"location": "kitchen", "action": "wait_for_shift_change"},
    {"location": "storage_room", "action": "move_crates"},
    {"location": "secret_tunnel", "action": "navigate_to_exit"}
  ],
  "success_probability": 0.72,
  "critical_points": ["guard_room"],
  "alternate_routes": 1,
  "knowledge_gaps": ["tunnel_destination"]
}
Query types:
  • Pathfinding and navigation
  • Social relationships and politics
  • Historical knowledge and lore
  • Predictive outcomes for actions

Best practice: Start with simple memory structures and gradually increase complexity. Too much memory can slow down AI processing and create inconsistent behaviors. Prioritize memories that are likely to affect gameplay directly.

Additional Implementation Tips

  • Start with simple goal/belief systems and expand as needed
  • Use consistent action types across similar NPCs
  • Consider performance impact of reasoning frequency
  • Implement graceful fallbacks for complex decisions
  • Test reasoning systems with various edge cases
  • Create a schema library of reusable output formats
  • Build in automatic retry logic for malformed responses
  • Consider rate limiting for decision-intensive game areas
  • Implement local decision caching for similar scenarios
  • Build debugging tools to visualize AI reasoning processes

Common Reasoning Patterns

Do ✓

  • Keep goals simple and focused
  • Update context frequently
  • Implement fallback behaviors

Don't ✗

  • Overload with too many goals
  • Ignore performance implications
  • Make decisions without context

Want to learn more?

Check out our guides on building NPCs and creating dialogue.

Back to your dashboard
View my API keys