Agentic recipes
These recipes build agent workflows from short lmscript files: agents with roles and goals, tasks run in order or by a manager, routing, tools, human approval, parallel reviewers, saved state and local knowledge. Each one is a single script you can read top to bottom. The script owns every step, every model call and every tool call; nothing runs in hidden framework state.
Each agent is an embedded model: the recipes call agent_prompt with a spec (provider, model, system prompt and optional tools), and the host runs the turn, keeps the session, runs any tools and records the turn. The result carries status, reply, tool_calls, files and usage. Set LLM_MODEL to a provider/model selector and export the matching key: OLLAMA_API_KEY, OPENROUTER_API_KEY, DEEPSEEK_API_KEY, ZHIPU_API_KEY or ANTHROPIC_API_KEY. Without LLM_MODEL the recipes use the first provider whose key is set. Keys are read from the environment and never printed.
These recipes need an lmscript release that includes the agent_* functions (agent_prompt, agent_open) and json_validate.
Recipes
- Triage router — Classify a support ticket with a validated JSON reply, then hand it to the matching specialist.
- Sequential crew — Run a researcher, a writer and an editor in order, each task seeing the previous output.
- Hierarchical manager — A manager agent assigns work to named workers until it declares the goal done.
- Tool agent — The model asks for built-in read-only tool calls in JSON; the script runs them and feeds the results back.
- Human checkpoint — A draft waits for an operator: y approves, n rejects, any other text is feedback for a revision.
- Fan-out and judge — Reviewers answer in parallel, then a judge returns one validated verdict with a reason.
- Stateful checkpoint — Save each step result to state.json so a rerun skips finished steps and resumes.
- Keyword knowledge — Score paragraphs of local files against a question and answer only from the best matches.
The concept map lists familiar agent-framework concepts and the recipe that covers each one.
Offline and live runs
Every recipe page shows two outputs. The offline output comes from examples/mock-model.lms, an lmmock script that answers like a chat model with fixed replies chosen by the agent role, so the site check can run every recipe without a key or network access. The live output is a recorded run against a real model; it is not rerun by the check.
Script: agentic.lms
# Shared helpers for the agentic recipes: an agent is a map, ask() is one agent_prompt turn on an embedded model.
# Switch provider or model with LLM_MODEL=provider/model; nothing else changes.
# With LLM_MODEL unset, the first registered provider whose key is set is used. Keys are never printed.
MODEL = env("LLM_MODEL", "");
BUDGET = 32768;
for (p in llm_providers()) {
usable = p.key_set == true || (env(p.key_env) != null && env(p.key_env) != "");
if (MODEL == "" && usable) { MODEL = p.id + "/" + p.default_model; }
if (MODEL != "" && starts_with(MODEL, p.id + "/") && p.max_output_tokens != null && p.max_output_tokens < BUDGET) { BUDGET = p.max_output_tokens; }
}
if (MODEL == "") { MODEL = "ollama-cloud/deepseek-v4.1-flash"; }
SLASH = index_of(MODEL, "/");
PROVIDER = slice(MODEL, 0, SLASH);
MODEL_ID = slice(MODEL, SLASH + 1, len(MODEL));
# An agent is its role plus the spec agent_prompt opens a session from: provider, model, system prompt and optional tools.
fn agent(role, goal) {
return {role: role, goal: goal, spec: {provider: PROVIDER, model: MODEL_ID,
system: f"You are the {role}. Your goal: {goal}. Reply with the requested output only."}};
}
fn with_tools(a, tools) {
spec = a.spec;
spec["tools"] = tools;
return {role: a.role, goal: a.goal, spec: spec};
}
# One turn. A failed turn stops the script with the role and the provider's error code.
fn turn(a, prompt, options) {
r = agent_prompt(a.spec, prompt, options);
if (r.dry_run) { return r; }
if (r.status != "ok") {
error(f"{a.role} failed: {r.status} {r.error == null ? "" : r.error.code}: {r.error == null ? "" : substr(r.error.message, 0, 240)}");
exit(1);
}
trace(a.role, r);
return r;
}
fn ask(a, prompt) {
r = turn(a, prompt, {max_tokens: BUDGET, retries: 2});
if (r.dry_run) { return f"(dry run: {a.role})"; }
return trim(r.reply);
}
# One JSON object that must match schema; the validator's errors are fed back for up to 2 retries.
# Some models wrap JSON in a code fence or add a sentence around it: keep the outermost braces.
fn json_body(text) {
first = index_of(text, "{");
last = last_index_of(text, "}");
if (first < 0 || last < first) { return text; }
return slice(text, first, last + 1);
}
fn ask_json(a, prompt, schema) {
request = prompt + "\nKeep every text field to one or two short sentences.\nReply with one JSON object matching this JSON Schema: " + json_stringify(schema);
last_errors = [];
for (attempt in range(0, 3)) {
r = turn(a, request, {max_tokens: BUDGET, retries: 2});
if (r.dry_run) { return null; }
value = json_parse_safe(json_body(r.reply), null);
checked = json_validate(value, schema);
if (checked.ok) { return value; }
last_errors = checked.errors;
request = prompt + "\nYour last reply was rejected: " + join(checked.errors, "; ") + "\nKeep every text field to one or two short sentences.\nReply with one JSON object matching: " + json_stringify(schema);
}
error(f"{a.role} gave no valid JSON after 3 attempts: {join(last_errors, '; ')}");
exit(1);
}
# Append one event per turn to trace.jsonl (role, tokens, steps; never prompt text or keys).
fn trace(role, r) {
tokens = r.usage == null ? null : r.usage.input_uncached + r.usage.output;
append_file("trace.jsonl", json_stringify({role: role, model: r.model_reported, tokens: tokens, steps: r.steps, latency_ms: r.latency_ms}) + "\n");
}
agent returns a map holding the role and the spec. with_tools adds a tools grant to the spec. turn runs one agent_prompt call and stops the script with the status and error code if the turn fails. ask returns the trimmed reply, with rate limits and server errors retried twice. ask_json asks for a JSON object, checks it with json_validate and sends the validation errors back for up to two more attempts. trace appends the role, token count and step count of each turn to trace.jsonl, never the prompt text.
Try it: download examples/agentic.lms with any recipe into an examples/ directory, export your provider key, and run the recipe with lmscript.