📄 Technical report: Testing Agentic LLM Workflows Without a Model (Zenodo, DOI 10.5281/zenodo.24689164) · Code: github.com/mafzalkalwardev/langgraph-agentic-workflow-demo

Agentic LLM workflows are fun to build and painful to test. Every node calls a model that is non-deterministic, costs money, and needs a key, so CI either skips the interesting parts or depends on an external API.

In langgraph-agentic-workflow-demo (MIT) I took a different approach: a deterministic mock chat model that honours the same prompt contract as a real model, so the whole graph (routing, retrieval, tool calls, critique, human approval and checkpointing) runs offline in tests. Swap in OpenAI or Ollama with one environment variable.

The graph

Built on LangGraph's StateGraph:

  1. supervisor asks the model for {"route": "rag" | "tools", "reason": ...}.
  2. retriever (RAG path) returns the top BM25 chunks from a bundled docs folder, dropping weak matches.
  3. agent (tools path) calls lookup and calculator via native tool calling, and a prebuilt ToolNode executes them, in parallel when the model emits several calls. Capped at four tool rounds.
  4. writer drafts an answer from the evidence only, with [source: file] / [tool: name] citations.
  5. critic returns {"score": 0..1, "feedback": ...}. Below 0.7 it loops back to the writer, up to max_iterations.
  6. human_review (optional) calls interrupt(). The thread is checkpointed and later resumed with approve or revise.
  7. finalize sets the final answer.

State uses two reducers: add_messages for the tool conversation and list concatenation for a human-readable trace.

Write the prompt contract down

The trick that made everything testable was writing down what each node expects from the model, and what happens when it doesn't get it:

Node Model must return Fallback
supervisor JSON with route, reason route rag
agent tool calls, or none to stop stop after 4 rounds
writer answer with citation tags critic scores it low
critic JSON with score, feedback score 0

The defensive parsing (default route, clamped score) also protects you from real models that occasionally return malformed JSON.

The mock model

MockChatModel subclasses LangChain's BaseChatModel, and bind_tools returns itself, so the agent node runs exactly the same code path as with a real provider. It looks at the system prompt to know which role it's playing:

  • Router: picks the tools route when the question mentions price, cost, percentages and similar cues.
  • Agent: emits lookup calls, then a calculator call built from the results, then stops.
  • Writer: composes an answer from the evidence, deliberately without citations on the first draft.
  • Critic: scores uncited drafts 0.5 and cited ones 0.9.

That deliberate first-draft flaw is the key: it makes the critic → writer revision loop run on every test, deterministically.

What the tests cover

28 pytest cases (22 functions, some parametrised) all pass locally. They check that:

  • the RAG route triggers exactly one revision,
  • the tool route makes two parallel lookups and then one calculator call,
  • max_iterations caps the loop,
  • interrupt-then-approve and interrupt-then-revise behave correctly,
  • resuming a thread with no pending interrupt raises (404 in the API),
  • a SQLite-checkpointed thread survives a brand-new graph instance,
  • the generated Mermaid diagram names every node.

Many assertions are on the trace lines, for example critic: score=0.50 must appear, so any change that silently alters the path through the graph breaks the tests.

How much does the orchestration cost?

With the mock answering in microseconds, timing a run measures LangGraph, state handling and persistence. I ran each route 200 times (after 10 warm-up runs) on an 8-vCPU VM, Python 3.13, LangGraph 1.2.24:

Checkpointer Route Median p95 Checkpoints per run
memory RAG 6.5 ms 7.8 ms 9
memory tools 12.4 ms 15.0 ms 13
SQLite RAG 10.7 ms 17.0 ms 9
SQLite tools 18.9 ms 22.2 ms 13

SQLite adds roughly 4–6 ms per question. Next to real model latency that's noise, but now you know.

A bug I found while writing this up

The calculator walks the Python AST and only allows numbers and basic operators, and it rejects exponents above 100. But it doesn't bound intermediate result size: ((9**99)**99)**99 produced a ~3-million-bit integer in 0.16 s, and one more level, 23 characters, didn't finish in 100 s. Since tool arguments come from the model (which may echo user input), the fix is to cap operand or result size too. Lesson: "safe eval" needs resource limits, not just a syntax whitelist.

Lessons

  • Inject the model. make_nodes(llm) and build_graph(llm, checkpointer) are what let tests swap in the mock and a temp SQLite file.
  • Make the mock fail on purpose so the failure paths get exercised.
  • Keep tools offline and deterministic so you can assert exact values (990 − 290 = 700).
  • Bound every loop.
  • A trace list is cheap observability.

The mock tests the plumbing, not the intelligence. You still need evaluations with real models to know whether they follow the contract and answer faithfully.

FAQ

Can I run this without an OpenAI key?
Yes. The default provider is the mock. pip install -e ".[dev]", then python -m demo or pytest.

How do I use a real model?
Set LLM_PROVIDER=openai (with OPENAI_API_KEY) or LLM_PROVIDER=ollama with a tool-calling model such as llama3.1 or qwen2.5.

How does human-in-the-loop work in LangGraph here?
The human_review node calls interrupt(). The checkpointer saves the thread under its thread_id, and you resume with Command(resume={"action": "approve"}) or "revise" plus feedback.

Do interrupted threads survive a restart?
With CHECKPOINTER=sqlite, yes. A test resumes a thread from a new graph instance.

Why BM25 instead of embeddings?
No model or key needed, deterministic results, and good enough for a small document set.

Is the API production-ready?
It's a reference implementation: no authentication, and anyone with a thread id can resume it. Add auth before exposing it.

I'm Muhammad Afzal Kalwar, a Rust and Python automation engineer and founder of INDUS Web Agency in Islamabad. The full technical report is on Zenodo: doi.org/10.5281/zenodo.24689164. Code: github.com/mafzalkalwardev/langgraph-agentic-workflow-demo.