Stateful checkpoint
Save each step result to state.json so a rerun skips finished steps and resumes.
The flow has three steps: outline, draft and subject line. After each step the whole state is written to state.json. On the next run finished steps are skipped and their saved results are reused. Set AGENTIC_FAIL_AT=draft to simulate a failure, then run again in the same directory to see it resume.
What it maps to
Flow state and checkpointing: an explicit state map saved after every step. Resume: rerun the same script in the same directory. See the concept map.
Script: stateful-checkpoint.lms
# Stateful checkpoint: each step's result is saved to state.json, so a rerun skips finished steps.
import "agentic.lms";
state_file = "state.json";
state = file_exists(state_file) ? read_json(state_file) : {topic: ARGV.get(0, "a weekly team status email"), done: {}};
steps = [
{name: "outline", who: agent("outliner", "outline in 3 bullets"), todo: "Outline: "},
{name: "draft", who: agent("drafter", "write from an outline"), todo: "Write at most 80 words from this outline: "},
{name: "subject", who: agent("subject writer", "write one subject line"), todo: "Write a subject line for: "}
];
input = state.topic;
for (s in steps) {
if (state.done.has(s.name)) {
println(f"{s.name}: skipped (saved)");
} else {
if (env("AGENTIC_FAIL_AT", "") == s.name) { error(f"simulated failure at {s.name}; rerun to resume"); exit(1); }
state.done[s.name] = ask(s.who, s.todo + input);
write_json(state_file, state);
println(f"{s.name}: done");
}
input = state.done.get(s.name, "");
}
println(f"subject: {state.done.subject}");
It imports the shared helpers from examples/agentic.lms, shown on the recipe index.
Run it
export OLLAMA_API_KEY=...
LLM_MODEL=ollama-cloud/deepseek-v4.1-flash lmscript examples/stateful-checkpoint.lms
Output (offline mock)
The site check runs this recipe against examples/mock-model.lms, a deterministic stand-in for the model, and compares this output.
outline: done draft: done subject: done subject: Status: recipes shipped, embeddings next
Live run
Recorded on 2026-11-01 with ollama-cloud/deepseek-v4.1-flash. It made 3 model calls and used 1390 tokens. Live replies differ from run to run.
outline: done draft: done subject: done subject: Weekly Team Status: On Track; Analytics Blocker & QA Decision Needed
Previous: Fan-out and judge · Next: Keyword knowledge
Try it: download examples/stateful-checkpoint.lms and examples/agentic.lms into an examples/ directory, export the key for your provider, then run the command above. Delete state.json to start over.