Newer ideas: authoring and repair with a model

A language model can draft a behavior tree from a sentence and propose a repair from a playtest report. Let it, and keep it honest: the validator answers its mistakes, scenarios you wrote first decide whether the tree is right, and a person reads the diff.

A language model is a program that writes text from a prompt, such as Claude or GPT. Ask one for “a ferryman who carries passengers across the river and stays ashore when it floods” and it will write a behavior tree file in seconds. It will also, quite often, write one that does not load: a node type that does not exist, a parameter spelt the way it guessed, an action with children. This page is about using a model where it helps (a first draft, a proposed repair) and putting checks around it that do not depend on the model being right.

Why trees suit a model

A behavior tree file is a good thing to ask a model for, for the same reasons it is a good thing to give a designer:

  • A small vocabulary. A tree is built from a dozen node types (the ones Building blocks explains) and the custom nodes the tree declares. A model that knows the format has little to invent.
  • A machine that says what is wrong. The format has a schema, and beetree validate names each mistake with its path in the tree and what would be right. A model reads that message as well as a person does, so it can fix its own draft.
  • Behavior you can check without the game. beetree simulate runs the tree with the answers you choose for its leaves, so whether the tree does the right thing in a situation is a command with an expected answer, not an opinion.
  • Changes you can read. A tree is a file. A change to it is a diff a person can review in a minute, which is not true of the weights inside a learned policy.

The research on generating trees with models (in the sources below) comes to the same place: the model’s draft is cheap and often wrong, and the checks around it decide what reaches the game.

The loop

Authoring with a model is a loop of five steps, and only the first one is the model’s:

  1. Draft. The model writes a tree from a description of the character and the custom nodes your game has.
  2. Validate. beetree validate checks the draft. Its errors go back to the model, which writes the next draft. Stop after a few rounds: a model that cannot fix an error in three tries needs a person, not a fourth try.
  3. Run the scenarios. A scenario is a situation and what the tree must do in it: “the river is in flood, so the ferryman ties up the boat”. You write the scenarios before the model writes the tree, as commands that pass or fail. A draft that validates but fails a scenario goes back to the model with the scenario’s name.
  4. Review the diff. A person reads what changed (beetree diff) and decides whether it is what they meant. The scenarios cover what you thought of; the review covers the rest.
  5. Generate. Only now does the tree become engine code.

Repair with a model is the same loop, started from a report of something going wrong in play instead of from a description. The report becomes a new scenario first, so the bug is a failing command before anyone changes the tree. Then the model proposes a change, and steps 2 to 5 decide whether it stays.

The model works at authoring time, on the file, never inside the running game. A tree that a model rewrites while the game runs has had no validation, no scenarios and no review, and the game’s frame cannot wait seconds for an answer.

A draft, and what the validator says

Here is part of a first draft for the ferryman, written the way a model often writes it: it guessed a parameter’s name, invented a node type that waits, and gave an action children.

custom_nodes:
  - name: RiverBelow
    type: condition
    parameters:
      - {name: level, type: float, default: 2.0}
# ...
      children:
        - {type: condition, name: river_safe, node: RiverBelow, parameters: {max_level: 2.0}}
        - {type: wait_until, name: wait_for_passenger, node: PassengerWaiting}
        - type: action
          name: cross
          node: Row
          parameters: {to: far_bank}
          children:
            - {type: action, name: row_back, node: Row, parameters: {to: near_bank}}

beetree validate answers with every mistake at once, each with where it is and what would be right:

Validation errors in draft.beetree.yaml:
  - tree.children[0].children[0]: parameter "max_level" of "river_safe" is not declared by custom node "RiverBelow" (declared parameters: level)
  - tree.children[0].children[1]: unknown node type "wait_until" (valid types: action, active_selector, condition, decorator, parallel, random_selector, random_sequence, selector, sequence, subtree, utility_selector)
  - tree.children[0].children[2]: leaf node "action" cannot have children
Error: found 3 validation error(s)

That text is the whole of the next prompt: “fix these errors”. Each line says what to change, so the model does not have to guess again. The second draft uses level, checks for a waiting passenger with a condition, and makes rowing across a sibling of boarding instead of its child.

A tree you can run

This is the ferryman after the validator’s round. Save it as ferryman.beetree.yaml.

# file: ferryman.beetree.yaml
version: "1.0"
metadata:
  name: ferryman
  description: "Carries passengers across the river, and ties up the boat when the river floods or nobody is waiting"
  tags: [newer-ideas, authoring, repair]
custom_nodes:
  - name: RiverBelow
    type: condition
    description: "Succeeds when the river is lower than level metres"
    parameters:
      - {name: level, type: float, default: 2.0}
  - name: PassengerWaiting
    type: condition
    description: "Succeeds when someone is waiting on the jetty"
  - name: Board
    type: action
    description: "Lets the waiting passenger into the boat"
  - name: Row
    type: action
    description: "Rows the boat to a bank; running until it lands"
    parameters:
      - {name: to, type: string, default: far_bank}
  - name: TieUp
    type: action
    description: "Ties the boat to the jetty and waits ashore"
tree:
  type: selector
  name: ferryman_root
  children:
    - type: sequence
      name: ferry
      children:
        - {type: condition, name: river_safe, node: RiverBelow, parameters: {level: 2.0}}
        - {type: condition, name: passenger_waiting, node: PassengerWaiting}
        - {type: action, name: board, node: Board}
        - {type: action, name: cross, node: Row, parameters: {to: far_bank}}
    - {type: action, name: tie_up_boat, node: TieUp}

It validates. Whether it is right is a different question, and the scenarios answer it. Each scenario below names the line the trace must contain (the leaf that decided) and the world, as --override answers for the leaves. Save it as accept.sh:

#!/usr/bin/env bash
# Usage: ./accept.sh ferryman.beetree.yaml
# Each scenario: the leaf that must decide, then the world as overrides.
tree=$1 failed=0
expect() {
  local want=$1; shift
  local args=() o
  for o in "$@"; do args+=(--override "$o"); done
  if beetree simulate "$tree" "${args[@]}" 2>/dev/null | grep -qF "$want"; then
    echo "pass: $want when $*"
  else
    echo "FAIL: $want when $*"; failed=1
  fi
}
expect "● ! tie_up_boat → SUCCESS" river_safe=FAILURE
expect "● ! tie_up_boat → SUCCESS" passenger_waiting=FAILURE
expect "● ! cross → RUNNING" cross=RUNNING
exit $failed
chmod +x accept.sh
./accept.sh ferryman.beetree.yaml
pass: ● ! tie_up_boat → SUCCESS when river_safe=FAILURE
pass: ● ! tie_up_boat → SUCCESS when passenger_waiting=FAILURE
pass: ● ! cross → RUNNING when cross=RUNNING

The flood scenario is the one a model’s draft most often gets wrong, by checking the river after boarding or not at all. Here is its trace, so you can see what the check reads:

beetree simulate ferryman.beetree.yaml --override river_safe=FAILURE
Simulation: ferryman_root → SUCCESS
──────────────────────────────────────────────────
▶ ? ferryman_root
  ▶ → ferry
    ● ¿ river_safe → FAILURE
  ◀ → ferry → FAILURE
  ● ! tie_up_boat → SUCCESS
◀ ? ferryman_root → SUCCESS

accept.sh is the same kind of check as the golden traces on Testing and simulation, cut down to the one line that matters in each situation, so that a model’s harmless renaming of a sequence does not fail it.

Repair from a playtest

In play, the ferryman keeps pushing off in a boat with a hole in it. The playtest log (the engine code below writes it) says:

Playtest of ferryman
tick 812: cross FAILURE 3 times in a row; boat_leaking=true river_level=1.2

First, the report becomes a scenario: with a leaking boat, the ferryman must mend it. Add it to accept.sh above the others:

expect "● ! mend_boat → RUNNING" boat_leaking=SUCCESS mend_boat=RUNNING

Against the current tree it fails, because the tree has no boat_leaking to answer: the bug is now a failing command. Then the model gets the tree, the report and the failing scenario, and proposes this, which validates on the first try:

# file: ferryman-v2.beetree.yaml
version: "1.0"
metadata:
  name: ferryman
  description: "Carries passengers across the river, and ties up the boat when the river floods or nobody is waiting"
  tags: [newer-ideas, authoring, repair]
custom_nodes:
  - name: RiverBelow
    type: condition
    description: "Succeeds when the river is lower than level metres"
    parameters:
      - {name: level, type: float, default: 2.0}
  - name: PassengerWaiting
    type: condition
    description: "Succeeds when someone is waiting on the jetty"
  - name: BoatLeaking
    type: condition
    description: "Succeeds when the boat has a leak"
  - name: Mend
    type: action
    description: "Patches the boat at the jetty; running until the patch is dry"
  - name: Board
    type: action
    description: "Lets the waiting passenger into the boat"
  - name: Row
    type: action
    description: "Rows the boat to a bank; running until it lands"
    parameters:
      - {name: to, type: string, default: far_bank}
  - name: TieUp
    type: action
    description: "Ties the boat to the jetty and waits ashore"
tree:
  type: selector
  name: ferryman_root
  children:
    - type: sequence
      name: mend
      children:
        - {type: condition, name: boat_leaking, node: BoatLeaking}
        - {type: action, name: mend_boat, node: Mend}
    - type: sequence
      name: ferry
      children:
        - {type: condition, name: river_safe, node: RiverBelow, parameters: {level: 2.0}}
        - {type: condition, name: passenger_waiting, node: PassengerWaiting}
        - {type: action, name: board, node: Board}
        - {type: action, name: cross, node: Row, parameters: {to: far_bank}}
    - {type: action, name: tie_up_boat, node: TieUp}

The review starts with what changed:

beetree diff ferryman.beetree.yaml ferryman-v2.beetree.yaml
3 change(s):
  + custom_nodes.BoatLeaking: condition
  + custom_nodes.Mend: action
  + tree.children.mend: → mend (sequence)

Three additions, all about the leak, and nothing else touched: that is a repair a person can approve in a minute. Now the scenarios, against the new tree:

./accept.sh ferryman-v2.beetree.yaml
pass: ● ! mend_boat → RUNNING when boat_leaking=SUCCESS mend_boat=RUNNING
FAIL: ● ! tie_up_boat → SUCCESS when river_safe=FAILURE
FAIL: ● ! tie_up_boat → SUCCESS when passenger_waiting=FAILURE
FAIL: ● ! cross → RUNNING when cross=RUNNING

The new scenario passes, and the old three fail. The tree is not wrong: the simulator answers SUCCESS for every leaf a scenario does not mention, so in the old scenarios the boat now leaks, and the ferryman mends it. A scenario describes the whole world, and the world now has a boat that can leak. Say so in each of them:

expect "● ! mend_boat → RUNNING" boat_leaking=SUCCESS mend_boat=RUNNING
expect "● ! tie_up_boat → SUCCESS" boat_leaking=FAILURE river_safe=FAILURE
expect "● ! tie_up_boat → SUCCESS" boat_leaking=FAILURE passenger_waiting=FAILURE
expect "● ! cross → RUNNING" boat_leaking=FAILURE cross=RUNNING

All four pass against the repair, and the first fails against the old tree, which is the proof that the repair fixed the report. Had the model also “tidied” the flood check while it was there, the review or the flood scenario would have caught it. This is the step a model is worst at and a person is best at: deciding whether a failing check means the tree is wrong or the check is.

Engine code

The repair loop starts from a report, and the report starts in the game. Watched wraps a node you care about while people play. When the node fails several ticks in a row, it writes one note to a PlaytestLog: the tick, the node’s name, how many times it failed, and the facts your game gives it (a function returning text such as boat_leaking=true river_level=1.2). It writes once when the count reaches the threshold, not every tick after, so a stuck agent makes one line, not thousands. Report is the text a person reads, or pastes to a model beside the tree file.

The samples build on the Node and Status of Building blocks.

Agnostic

PlaytestLog: threshold = 3, tick (set by the game), notes

Watched(name, child, log, facts):
    tick():
        status = child.tick()
        if status == Failure: failuresInARow += 1
        else: failuresInARow = 0
        if failuresInARow == log.threshold:          // once, not every tick after
            log.notes.add("tick " + log.tick + ": " + name + " FAILURE "
                          + failuresInARow + " times in a row; " + facts())
        return status

report(treeName) = "Playtest of " + treeName + "\n" + notes, one a line

Unity C#

// Collects one note for each node that kept failing while someone played.
public class PlaytestLog
{
    public int Threshold = 3;   // failures in a row before a note is written
    public int TickNumber;      // set by the game before each tick
    public readonly System.Collections.Generic.List<string> Notes =
        new System.Collections.Generic.List<string>();

    public void Note(string line) => Notes.Add("tick " + TickNumber + ": " + line);

    // What a person reads, or pastes to a model beside the tree file.
    public string Report(string treeName) =>
        "Playtest of " + treeName + "\n" + string.Join("\n", Notes);
}

// Wraps a node and writes a note when it fails Threshold ticks in a row.
public class Watched : Node
{
    readonly string name;
    readonly Node child;
    readonly PlaytestLog log;
    readonly System.Func<string> facts;   // the game's own state, as text
    int failuresInARow;

    public Watched(string name, Node child, PlaytestLog log, System.Func<string> facts)
    {
        this.name = name;
        this.child = child;
        this.log = log;
        this.facts = facts;
    }

    public override Status Tick()
    {
        var status = child.Tick();
        failuresInARow = status == Status.Failure ? failuresInARow + 1 : 0;
        if (failuresInARow == log.Threshold) // once, not every tick after
            log.Note(name + " FAILURE " + failuresInARow + " times in a row; " + facts());
        return status;
    }
}

// The ferryman's crossing, watched during a playtest:
// var cross = new Watched("cross", rowAcross, playtest,
//     () => "boat_leaking=" + boat.Leaking.ToString().ToLower() + " river_level=" + river.Level);
// Debug.Log(playtest.Report("ferryman")) when the session ends.

GDScript

# Collects one note for each node that kept failing while someone played.
class PlaytestLog:
    var threshold := 3        # failures in a row before a note is written
    var tick_number := 0      # set by the game before each tick
    var notes: Array[String] = []

    func note(line: String) -> void:
        notes.append("tick %d: %s" % [tick_number, line])

    # What a person reads, or pastes to a model beside the tree file.
    func report(tree_name: String) -> String:
        return "Playtest of %s\n%s" % [tree_name, "\n".join(notes)]

# Wraps a node and writes a note when it fails threshold ticks in a row.
class Watched extends BTNode:
    var node_name: String
    var child: BTNode
    var playtest: PlaytestLog
    var facts: Callable   # the game's own state, as text
    var failures_in_a_row := 0

    func _init(n: String, c: BTNode, p: PlaytestLog, f: Callable):
        node_name = n
        child = c
        playtest = p
        facts = f

    func tick() -> Status:
        var status := child.tick()
        failures_in_a_row = failures_in_a_row + 1 if status == Status.FAILURE else 0
        if failures_in_a_row == playtest.threshold:   # once, not every tick after
            playtest.note("%s FAILURE %d times in a row; %s" % [node_name, failures_in_a_row, facts.call()])
        return status

# The ferryman's crossing, watched during a playtest:
# var cross := Watched.new("cross", row_across, playtest,
#     func(): return "boat_leaking=%s river_level=%.1f" % [boat.leaking, river.level])
# print(playtest.report("ferryman")) when the session ends.

Unreal C++

// One note: a node that kept failing, when, and what the game said about it.
struct FPlaytestNote
{
    int64 Tick = 0;
    FString Node;
    int32 FailuresInARow = 0;
    FString Facts;   // the game's own state, as text
};

// Collects one note for each node that kept failing while someone played.
class FPlaytestLog
{
public:
    int32 Threshold = 3;   // failures in a row before a note is written
    int64 TickNumber = 0;  // set by the game before each tick
    TArray<FPlaytestNote> Notes;

    // What a person reads, or pastes to a model beside the tree file.
    void Report(const FString& TreeName) const
    {
        UE_LOG(LogTemp, Display, TEXT("Playtest of %s"), *TreeName);
        for (const FPlaytestNote& Note : Notes)
        {
            UE_LOG(LogTemp, Display, TEXT("tick %lld: %s FAILURE %d times in a row; %s"),
                   Note.Tick, *Note.Node, Note.FailuresInARow, *Note.Facts);
        }
    }
};

// Wraps a node and writes a note when it fails Threshold ticks in a row.
class FWatched : public FNode
{
public:
    FString Name;
    TSharedPtr<FNode> Child;
    FPlaytestLog* Log = nullptr;
    TFunction<FString()> Facts;
    int32 FailuresInARow = 0;

    EStatus Tick() override
    {
        const EStatus Status = Child->Tick();
        FailuresInARow = Status == EStatus::Failure ? FailuresInARow + 1 : 0;
        if (FailuresInARow == Log->Threshold) // once, not every tick after
        {
            Log->Notes.Add(FPlaytestNote{Log->TickNumber, Name, FailuresInARow, Facts()});
        }
        return Status;
    }
};

Wrap the actions whose failure would surprise you (rowing, opening a door, reaching a cover point), not every node: a condition that fails is usually doing its job. Keep the watching in playtest and development builds. The facts are the part a model cannot work out from the tree: the tree says cross failed, and only the game knows the boat was leaking.

What BeeTreeCraft shows today

  • The MCP server gives a model the steps of the loop as tools: validate_tree, simulate_tree, diff_trees, render_tree and generate_code, without an account. Its prompt design_behavior_tree walks a model through reading the format, writing a tree, validating it and simulating its failure paths. claude mcp add --transport http beetreecraft https://api.beetreecraft.com/mcp connects Claude Code.
  • beetree validate names a misspelt parameter, a value of the wrong type, an unknown node type and a leaf with children, each with its path. It does not yet reject a node: that names a custom node the tree does not declare, so a model’s misspelt custom node name validates; a scenario that expects that leaf to decide will catch it.
  • beetree simulate runs one tick, and answers SUCCESS for any leaf you do not override, as the repair above shows. A scenario that needs several ticks is one command per tick.
  • Neither the builder nor the CLI calls a model. The model is yours, through the MCP server or by running the CLI commands above; BeeTreeCraft supplies the checks.

Where it comes from

Izzo, Bardaro and Matteucci’s BTGenBot (2024) fine-tunes small language models to write behavior trees from a task description, and checks what they write in stages: syntax, a validator, a simulator, then a real robot, across nine tasks. Ao and colleagues’ LLM-as-BT-Planner (2024) compares ways of prompting a model to write trees for long assembly tasks, and measures how often each produces a tree that works. Model-written trees have older relatives: Colledanchise, Parasuraman and Ögren’s Learning of Behavior Trees for Autonomous Agents (2015) evolves trees by genetic programming, and Paduraru and Paduraru’s Automatic difficulty management and testing in games (2019) evolves them to find exploits in a game. In each, generated trees are only as good as the checks that select them. More reading is on the Resources page.

Next, connect the MCP server to your model and ask it for a character of your own, with your scenarios written first; or open the Build hub, paste the ferryman into the builder and use Step through to play the leaking boat.