MeridianDocsSign in
Internals

Agent internals

The tool loop, streaming, the prompt, card answers and the offline guide — how a turn actually runs.

The agent lives in backend/agent and knows nothing about HTTP or SQL. The server gives it a model (ChatModel) and a ToolHost that executes tools; it returns the blocks to persist and streams events as it goes.

The tool loop (runner.rs)

messages = history + your message
repeat up to 12 times:
    stream the model (system prompt, messages, 22 tool schemas)
        text  → forward as `delta`, collect
        tool  → collect the call
    if no tool calls: done
    for each call:
        emit `tool` running → ToolHost.call(name, input) → emit `tool` done/error
        emit any cards it returned
    send the tool results back as the next message
    if a tool asked you something (a choice, confirm, upload or invite card): stop and wait
  • Provider-native content (for Anthropic: thinking blocks with signatures, and tool calls) is replayed unchanged within a turn, as the API requires.
  • Invalid tool JSON returns an error result, so the model can retry.
  • A refusal ends the turn with a short, polite message.

The prompt (prompt.rs)

A stable instruction block covers the agent's role, how to lead, using cards, propose-before-acting, citing requirement IDs and never reproducing licensed text. It's followed by who you are, today's date, and a JSON snapshot of the programme from domain::overview.

History (blocks.rs)

Stored messages are flattened to text for the model. Cards become one line, such as [showed choice card crd_…: What does your company do? options: … status answered], and answers become [answered card crd_…: Software / SaaS]. The model keeps the meaning of earlier cards without provider-specific replay. The last 40 messages are sent.

Card answers (server/host.rs::answer_card)

When your message carries an action, the server applies it before the agent runs:

CardOn answer
choiceMarks it answered; if meta.save_to_profile is set, writes the answer to that profile field
confirmOn approve, runs the action (enable framework, complete task, publish policy) and stores its result (XP, milestone, level) on the card
uploadLinks the attached evidence to the card's task
inviteCreates the invite with the role and tasks; stores the link

The updated card is streamed as card_update. Answering a finished card returns 409, so actions can't run twice.

Shadow (llm/shield.rs)

Shielded wraps the active model:

  • Outgoing: masks the system prompt, every text part, tool results and tool inputs. Provider-native content is skipped, because it was produced from masked input already.
  • Incoming: streams through Shadow's unmasker, which holds back a short tail so a fake value split across two chunks is still restored. Tool-call inputs are unmasked before execution.

The offline guide (guide.rs)

A deterministic state machine over the same ToolHost:

  1. If a profile field is missing, it asks the next onboarding question (ask_user with save_to_profile).
  2. If no framework is enabled, it recommends one (propose_enable_framework).
  3. Otherwise it matches your text against task titles and simple intents (policy, evidence, invite, progress), and falls back to the next task.

Adding a tool

See Extending Meridian.