LLM chains¶
lAIfe reasons through a small set of LLM chains, each with one job.
They are built on llm_core's structured chain and prompt loader, so every chain returns a validated pydantic object rather than free text.
The chains live in the llm package and in a few entity modules.
The shared pattern¶
Each chain pairs an input model with a prompt template and an output model.
- The input is a
BaseModelKwargswhose field names are the variables the prompt must contain. The structured chain derives the required variables from these fields, so the prompt and the code cannot drift apart. - The prompt template is loaded by the
llm_coreprompt loader. - The output is a pydantic model, so the result is validated on the way out.
There is no wrapper class per chain: the brain and the world judge build each StructuredLLMChain from its two models and its prompt.
The modules listed below hold the models.
The template text itself lives in the prompts package, one folder per chain with versioned vN.jinja files.
A chain points at its folder by name; the loader resolves the template from there, so prompt wording can change without touching the chain code.
The player's brain¶
Each player has one PlayerBrain, which owns the player's four chains: action, planner, replier and mission generator.
Player calls one brain method per decision (next_action, plan, reply, propose_mission), passing plain values, and applies the result to its own state and mission.
It holds no world queue, so a reply, which the world requests from inside its own loop, cannot reach the world.
The world judge¶
WorldJudge is the same split on the world's side.
It owns three chains on one input and output model, one per judged request: build, craft and complete.
WorldRunner builds it, runs the free checks such as collision before calling it, and turns the verdict into a world response.
Timing¶
Every chain is built with a name and on_call=log_llm_call (in laife.meta.logger), so each call that returns writes one llm_call record with the name as its stage, the model and the elapsed seconds.
Player and turn are not passed to the chain: they come from slog.contextualize, set by Player.play for each turn and by WorldRunner.simulate around each request, so a judge's call is logged as the requester's.
A reply runs inside the world's task while it handles the sender's request; Player.receive_message sets its own context, or the reply would be logged as the sender's.
A new chain is timed by passing the same two arguments.
The chains¶
- Action picker -
entities/action.py, called byPlayerBrain.next_action. Given the observation and the current mission, returns the next action. This is the core decision step of a turn. - Planner -
llm/player_planner.py. Decomposes a hard mission into ordered sub-missions, using the mission history as context. Triggered by the plan action. - Replier -
llm/player_replier.py. Produces an in-character reply to a message from another player. It is called from inside the world's interaction routing, so it stays free of world I/O. - Mission generator -
llm/mission_generator.py. Proposes a new mission objective from the current world observation, so a player without a goal can find one. - World judge -
entities/world_judge.py. Decides whether a build, a craft or a reported mission completion is valid and returns a success flag with feedback. This is the world's reasoning, not the player's.
Missions as data¶
The mission and its history are plain models in llm/mission.py.
Missions carry an objective, a status, and a list of action and result entries, and they can nest.
They render to prompt text so the brain and planner always see the full context they have accumulated.
Configuration¶
Which model each chain uses, and with what settings, comes from the LLM service params. See params and config for how chat, embedding, and search settings are assembled per environment.