Skip to content
Forward Deployed

Forward Deployed
T7 exercise

Exercise: one task as a workflow and as a ReAct agent

Track
Agent Foundations
By
Sourav Banerjee
Last updated
30 Nov 2026

Report an issue

01 Context

Problem statement

Larkspur Cloud's support team wants an assistant that answers customer questions from the handbook. One engineer wants a fixed workflow: retrieve four passages, answer once. Another wants a ReAct agent that searches as often as it needs. You build both on the same model, retriever and refusal rule, run them on the same 20 golden questions, and compare what each one costs and where each one fails.

The rule you apply comes from lesson 1 of T7.3: use a workflow when you can draw the task's steps before it runs, and pay for an agent only where its steps depend on what it finds. The agent has to earn its extra calls with answers the workflow gets wrong.

02 Outcomes

What you will learn

  • Build the same task as a fixed chain and as a create_agent ReAct agent, with everything else held fixed.
  • Count model calls and tokens per question from each reply's usage_metadata.
  • Cap an agent with ModelCallLimitMiddleware and read what a capped run returns.
  • Score evidence recall over every search, and correctness against key facts from the reference answers.
  • Label each failed run by kind and decide which design the task deserves.

03 Setup

Before you start

Lessons

Dataset

The Larkspur Cloud handbook (13 files), its 30-question golden set and a key-fact file for the 15 in-corpus questions used here, in data/. The run uses 20 questions: 7 lookups, 6 multi-hop, 2 numeric and all 5 out-of-corpus questions.

Code

load_handbook, load_jsonl, tfidf_retriever and is_hit from src/lab_kit.py, RecursiveCharacterTextSplitter from langchain_text_splitters, create_agent and ModelCallLimitMiddleware from langchain, and ChatOllama from langchain_ollama.

Environment

The course's default environment (uv run jupyter lab), or Colab after cloning the repository. Everything runs offline through Ollama with qwen2.5:3b pulled (ollama pull qwen2.5:3b). No API keys. Cost is counted in tokens from each reply's usage_metadata, not in money, because a local model has no price per token. To get dollars on a hosted model, multiply the token counts by its price page.

04 Architecture

Experiment workflow

The model, the chunks, the retriever (TF-IDF, k = 4), the refusal rule and the questions stay fixed. Only who picks the next step changes.

Diagram: Load the handbook and 20 golden questions; Recursive 500 / 50 chunks, TF-IDF k = 4; Workflow: retrieve once, answer once; ReAct agent: search_handbook tool, cap 6 model calls; Meter: model calls, tokens in and out, seconds; Evidence recall and key-fact correctness; Label each failed run; Keep the workflow or the agentDiagram: Load the handbook and 20 golden questions; Recursive 500 / 50 chunks, TF-IDF k = 4; Workflow: retrieve once, answer once; ReAct agent: search_handbook tool, cap 6 model calls; Meter: model calls, tokens in and out, seconds; Evidence recall and key-fact correctness; Label each failed run; Keep the workflow or the agent

Extension: the same agent at a cap of 3 model calls.

05 Build

Implementation tasks

  1. Task 1: Load the data

    Load the handbook and the golden set from data/larkspur/, split the handbook with the recursive splitter at 500 characters and 50 of overlap, and build one TF-IDF retriever at k = 4. Both systems share it.

  2. Task 2: Count calls and tokens

    Write a callback that counts chat-model calls and adds up input_tokens and output_tokens from each reply's usage_metadata. It is the Meter from the T7.3 lab. Pass a fresh one in config for every run.

  3. Task 3: Build the workflow

    Retrieve the top 4 chunks for the question and make one model call that answers from them, or replies exactly I don't know. when they do not hold the answer.

    ChatOllama(model="qwen2.5:3b", temperature=0)
    
  4. Task 4: Build the agent

    Wrap the same retriever as a search_handbook tool and give it to create_agent with the same refusal rule, plus a line telling it to search again with other words when the first results miss. Cap it, and have the tool log every chunk it returns so you can score evidence over all its searches.

    create_agent(model=llm, tools=[search_handbook], system_prompt=AGENT_PROMPT,
                 middleware=[ModelCallLimitMiddleware(run_limit=6, exit_behavior="end")])
    
  5. Task 5: Score every run

    Evidence recall is the share of a question's evidence spans found in any retrieved chunk. On in-corpus questions, check the answer against the reference answer: data/key_facts.json lists each reference answer's facts as regular expressions, and the answer is correct when it matches all of them and is not the refusal. On out-of-corpus questions, only the refusal is correct. Give each failed run one label: stopped at the call cap, answered without searching, answered an out-of-corpus question, refused an answerable question, or wrong or incomplete answer. Then print every reply and read a few verdicts yourself.

    New here: the key-fact file and its regular expressions. Lesson 5 of T7.1 scores synthesis with "an LLM judge or a reference answer", and this is the reference-answer side, a few lines of re.search. We tried a llama3.2:3b judge first. It failed correct answers such as "A Starter account can run up to 3 servers" and passed a reply about rebooting for the outage question, so the key-fact check replaced it.

  6. Task 6: Compare and decide

    Put the two systems side by side: mean and largest model calls, mean tokens, mean seconds, evidence recall, correct answers and failed runs. Then read the per-question rows where they disagree.

  7. Task 7: Extension: a tighter cap

    Run the agent again with run_limit=3 and add it as a third row. Count the runs that stop at the cap, and check whether their evidence was already in the message list.

06 Measure

Record results

What you enter stays in this browser only.

SystemMean model callsLargest model callsMean tokens inMean tokens outEvidence recallCorrect of 20Failed runsFailure kinds
Workflow11
ReAct agent, cap 6
ReAct agent, cap 3 (extension)

Model calls and tokens come from the meter. Evidence recall is over the 15 in-corpus questions. Failure kinds lists the labels from task 05 with their counts.

07 Verify

Done when

0 of 4 done

What you enter stays in this browser only.

08 Debug

Common mistakes

Changing more than the driver

A different prompt, k or model in the agent turns the comparison into two changes at once.

Counting only the answers

A run stopped by the cap or answered without a search can still read well. Lesson 5 of T7.1 asks you to count those runs separately.

Trusting a grader you have not read

A small judge model, or a key-fact pattern that is too loose, can pass a wrong answer. Read the replies behind a sample of verdicts before you compare the rows.

Reading seconds as latency

The runs share one local Ollama server, so the seconds show which system is slower, not how fast either would be in production.

09 Transfer

Interview angle

“When would you build an agent instead of a fixed workflow?”

Show a model answer

When the next step depends on what the last one found, such as a second search worded from the first result. If you can draw the steps before the request arrives, a workflow is cheaper, easier to test and fails at a node you named. Measure both on a golden set before you pay for the loop.

10 Review

Worked solution

The worked solution is solution/p06_exercise_solution.ipynb (open in Colab). Locally, open it from exercise/solution/ so ../data/ resolves, and run it top to bottom with the default environment and Ollama running. In Colab, clone the repository first, because the notebook reads the corpus and src/lab_kit.py from it, and point ChatOllama at an Ollama server you can reach.

Show our run and the reasoning

Our run: qwen2.5:3b through Ollama 0.40.6, langchain 1.4.7, langgraph 1.2.18, langchain-ollama 1.1.0 and scikit-learn 1.9.1, offline.

System Mean model calls Largest model calls Mean tokens in Mean tokens out Evidence recall Correct of 20 Failed runs Failure kinds
Workflow 1 1 360 29 0.90 15 5 3 refused an answerable question, 2 wrong or incomplete
ReAct agent, cap 6 1.85 2 676 80 0.77 13 8 3 answered without searching, 3 wrong or incomplete, 2 refused an answerable question
ReAct agent, cap 3 (extension) 1.85 2 676 80 0.77 13 8 the same 8 runs as cap 6

The agent spent about twice the tokens per question (756 against 389) and got two fewer answers right. It never searched twice. Every run made one search or none, so no run used more than 2 model calls, and neither cap ever fired. The cap 3 row is identical to the cap 6 row for that reason. The loop that lesson 1 of T7.2 measures, a second search worded from the first result, did not happen once with this 3B model.

The failures differ in kind. The agent answered three questions without calling the tool. On g03 and g23 it replied "I don't know." from nothing, although the workflow's single retrieval found the evidence and answered both, so its evidence recall drops to 0.77 against the workflow's 0.90. On g26, an out-of-corpus question, the same skipped search happened to give the right refusal, so it counts as correct and as a failed run. On g15 and g21 it searched, got the evidence back, and still said the passages did not answer. The workflow's three failures were refusals with the evidence in front of it (g09, g18, g20). The agent answered g09 and g18 from the same chunks, which accounts for most of its wins.

Neither system could answer g02, "What happens if my server goes down?". Its retrieval returns the reboot passages and no evidence, and the agent, which could have searched again with other words, accepted the first result. g20 needs two passages and both systems refused. The seconds column (0.42 against 1.36 mean seconds, in the notebook) only shows that the agent is slower on a shared local server.

The workflow stays. On this task the agent paid for a second model call on almost every question and did not use the loop that would justify it. The one question where a second search could help, g02, is better fixed in retrieval (lesson 2 of T7.2's fixes, most effective first) than by raising the cap. All 5 out-of-corpus questions were refused by both systems.