Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:4thfever Cultivation world simulator LLM Driven Agent Decision

From Leeroopedia
Knowledge Sources
Domains AI_Agents, LLM_Applications, Game_AI
Last Updated 2026-10-22 14:00 GMT

Overview

A decision-making pattern where a Large Language Model serves as the reasoning engine for autonomous agents, converting assembled world context and an action catalog into structured action chains with thinking and objectives.

Description

LLM-Driven Agent Decision addresses the challenge of generating believable, context-aware behavior for non-player characters (NPCs) in a simulated world. Instead of hand-coded behavior trees or utility AI, the pattern delegates the decision to an LLM by:

  1. Filtering eligible agents: only agents with no current action and no queued plans need decisions
  2. Assembling context: the agent's state (position, sect, cultivation, relations), observable nearby agents, world info, available actions (filtered by feasibility), and world lore are packed into a structured prompt
  3. LLM call: a locale-specific template is filled with context and sent to the LLM, which returns JSON with action pairs, thinking text, and a short-term objective
  4. Response parsing: the LLM's JSON response is parsed into an ordered list of (action_name, params) tuples, with validation and fallback for malformed data
  5. Plan loading: the parsed action chain is loaded onto the agent as its plan queue, with the first action becoming the current action

This pattern enables emergent, narrative-rich NPC behavior without explicit scripting of every possible action sequence.

Usage

Use this principle when:

  • NPCs in a simulation need context-aware, varied decision-making that would be impractical to hand-code
  • The action space is large (30+ actions with parameters) and state-dependent
  • Narrative coherence matters: NPCs should have "thinking" and "objectives" that explain their actions
  • A test mode must produce deterministic, rule-based decisions without LLM calls
  • Multiple NPCs can be decided in parallel (concurrent LLM calls)

Theoretical Basis

The decision pipeline follows the observe-reason-act loop:

# Abstract algorithm description (NOT real implementation)
async def decide_for_agents(world, agents):
    results = {}
    # Parallel: one LLM call per agent
    tasks = [decide_one(world, agent) for agent in agents]
    responses = await asyncio.gather(*tasks)
    
    for agent, response in responses:
        # Parse action chain from LLM JSON
        pairs = parse_action_pairs(response["action_name_params_pairs"])
        thinking = response.get("avatar_thinking", "")
        objective = response.get("short_term_objective", "")
        emotion = parse_emotion(response.get("current_emotion"))
        
        if pairs:  # skip if no valid actions
            agent.load_decide_result_chain(pairs, thinking, objective)
            agent.emotion = emotion
            results[agent] = (pairs, thinking, objective)
    return results

async def decide_one(world, agent):
    # Assemble context
    actions = get_available_actions(agent)  # filtered by can_possibly_start()
    world_info = world.get_info(avatar=agent)
    observed = world.get_observable_avatars(agent)
    avatar_info = agent.get_expanded_info(co_region=observed)
    
    prompt = render_template("ai.txt", {
        "avatar_name": agent.name,
        "avatar_info": avatar_info,
        "world_info": world_info,
        "general_action_infos": actions,
        "world_lore": world.world_lore,
    })
    return agent, await call_llm("action_decision", prompt)

Key design properties:

  • Action filtering: only actions where `can_possibly_start()` returns True are included in the prompt, reducing the action space
  • Batch concurrency: all agents' decisions are dispatched concurrently via `asyncio.gather`
  • Structured output: LLM returns JSON with `action_name_params_pairs` (list of [name, params]), not free text
  • Emotion tracking: LLM also returns a `current_emotion` that updates the agent's emotional state
  • Test-mode fallback: in test mode, `call_llm_with_task_name` returns a rule-based fallback instead of calling the real LLM

Practical Guide

When implementing LLM-driven agent decisions:

  • Filter the action space: remove infeasible actions before presenting them to the LLM to reduce hallucination
  • Use locale-specific templates: the prompt template should be in the game's configured language
  • Parse defensively: LLM output may contain nulls, missing keys, or malformed pairs; validate and skip invalid entries
  • Batch for performance: use `asyncio.gather` to parallelize LLM calls across agents
  • Register test-mode fallbacks: every LLM task needs a deterministic fallback in test mode to avoid real API calls
  • Separate decision from execution: the decision phase only loads plans; the commit phase triggers start events; the execute phase runs the actions

Related Pages

Implemented By

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment