← Back to Agentic AI map
Lesson 1.11 · Agents From Scratch

Two Agents Talking

The simplest multi-agent setup: one agent’s output becomes the next agent’s input.

multi-agent

What you will be able to do

  • Build a two-agent pipeline with nothing but two functions
  • Give each agent a role through its system message
  • Explain what the agents share - and what they do not
  • Tell a fixed hand-off apart from a ReAct loop
  • Explain why an agent without tools is not doing research
  • Say what a reviewer agent can and cannot catch

The idea, in plain English

The simplest "multi-agent" setup needs no framework: two agents with different jobs, where the first one’s output goes into the second one’s prompt. In Python it is one function called with the result of another: notes = researcher_agent(topic), then article = writer_agent(topic, notes).

A system message gives each agent its role - one lists facts, the other writes an article. They share no memory: the only link between them is the text you pass along. That makes the pattern easy to follow - there is no hidden state anywhere.

It also has a blind spot. The "researcher" has no tools, so its facts come from the model’s memory - and the writer cannot tell a true note from a false one. We tested exactly that, and added a third agent to review the result. The real runs are below.

Worked example: A researcher agent hands off to a writer agent.

workflowHow a false fact travelsstep 1 / 4

1 - The notes contain a false fact

Four true points and one false one: "Local LLMs are encrypted by default, so a stolen laptop cannot expose your conversations." A tool-less researcher can produce exactly this kind of plausible error.

notes
5 bullets
false
1
looks false
no
source
none - no tools

A real run. One note handed to the writer was false - "local LLMs are encrypted by default". Step through what each agent did with it.

An everyday example: a reporter and an editor

A small newspaper has two people on a story:

  • The reporter writes five bullet points of notes. The editor turns the notes into a neat three-paragraph article.
  • They never talk. The only thing that passes between them is the sheet of notes.
  • But this reporter never leaves the desk - no phone calls, no visits. The notes come from memory. Most are right; one is wrong, and it looks just like the others.
  • The editor cannot check anything - the notes are all they have. Their job is to make the text read well, so the wrong fact comes out in smooth, confident sentences.
  • A proofreader then compares the article with the notes, finds every sentence matches, and approves it. True - it matches the notes. Nobody checked the notes against the world.
The story -> the code
The reporterresearcher_agent(topic) - one model call.
The editorwriter_agent(topic, notes) - another model call.
Each person’s jobEach agent’s system message.
The sheet of notes passed overThe hand-off: one agent’s output in the next one’s prompt.
They never talkNo shared memory - only the text you pass.
Notes from memory, no phone callsA researcher with no tools: generated facts.
The wrong fact, in smooth sentencesErrors get more confident as they pass along.
The proofreaderreviewer_agent: checks the article against the notes, not the truth.

The hand-off is just function calls

  • researcher_agent(topic) makes one model call and returns text.
  • writer_agent(topic, notes) makes another, with the notes pasted into its prompt.
  • That is the whole pattern: the output of one is the input of the next.
  • It works for any pair with separate jobs - a planner and a coder, an analyser and a report writer - and for longer chains: add a third function that takes the article.
Researcher, then writer
import ollama def researcher_agent(topic): response = ollama.chat( model="llama3.1", messages=[ {"role": "system", "content": "You are a researcher. Given a topic, list 4-5 key facts as short bullet points. No commentary."}, {"role": "user", "content": topic} ] ) return response["message"]["content"] def writer_agent(topic, research_notes): response = ollama.chat( model="llama3.1", messages=[ {"role": "system", "content": "You are a writer. Turn research notes into a short, friendly 3-paragraph article."}, {"role": "user", "content": f"Topic: {topic}\n\nResearch notes:\n{research_notes}"} ] ) return response["message"]["content"] topic = "Benefits of local LLMs for privacy" notes = researcher_agent(topic) print("--- Research notes ---") print(notes) article = writer_agent(topic, notes) print("\n--- Final article ---") print(article)
Output - from a real run
--- Research notes --- • Local processing reduces data transmission and minimizes exposure to potential attackers. • It allows for more effective data anonymization and pseudonymization. • Local LLMs can better comply with data localization regulations and laws. • Reduced reliance on cloud services and centralized infrastructure improves overall system security. • Local processing enables more precise and personalized decision-making, reducing the risk of biased or unfair outcomes. --- Final article --- The Importance of Local LLMs for Your Privacy When it comes to processing large amounts of data, it's crucial to prioritize privacy. ... ... you can ensure that sensitive information is kept private and confidential, even when sharing it with third-party services. <- not in the notes ... Perhaps most importantly, local LLMs enable more precise and personalized decision-making, reducing the risk of biased or unfair outcomes. <- the weakest note, promoted ... making it a win-win for both your organization and your customers.

Each role is a system message

  • The two functions are almost the same. The system message is what makes one a researcher and the other a writer.
  • So roles are cheap to create - and only as reliable as the model’s obedience to them.
  • The researcher followed "4-5 key facts as short bullet points" closely: five bullets every time, plus an opening line in one run of four.
  • The writer followed "3-paragraph article" less well: two of three articles added a title, and the one above added claims the notes never made - "even when sharing it with third-party services".

No shared memory, no loop

  • Each agent is a separate, one-shot call. There is no shared messages list.
  • The writer knows only what you put in its prompt. If it needs something, you have to pass it.
  • There is no loop either. A ReAct agent (Lesson 1.6) decides what to do next, again and again. This pipeline is two fixed calls in a fixed order.
  • So nothing can go back to the researcher for more facts, and nothing decides when the article is good enough.
Hand-off and ReAct
Hand-off (1.11)Fixed sequence of calls. Each agent one job, one call.
ReAct (1.6)One agent looping: decide, act, observe, repeat.

A researcher without tools is not researching

  • The researcher has no search, no files, no database. Its "facts" are whatever the model generates - the chatbot problem from Lesson 1.4, with a new name.
  • We asked it about the history of Python - a topic you can check - three times.
  • Next to correct dates, it said Python 2.0 introduced "a just-in-time compiler" (it did not).
  • It said Python 3.7 introduced "optional static typing and improvements to the print function" (type hints came in 3.5, the print function in 3.0).
  • It said Python 1.2 was "a major rewrite of the language" (it was not).
  • Every one was a crisp, confident bullet point - right and wrong look the same.
The researcher on a topic you can check
researcher_agent("The history of the Python programming language") - three runs • 1991: Python 0.9.1 is released. correct • 2000: Python 2.0 ... introduced a new garbage collector and a just-in-time compiler. wrong - no JIT • 2018: Python 3.7 was released, which introduced optional static typing and improvements to the print function. wrong - 3.5 and 3.0 • 1994: Python 1.2 is released, featuring a major rewrite of the language. wrong • Python 3.0 was released in 2008, ... breaking backward compatibility. correct

Watch out: Calling an agent a "researcher" does not make it one. Without a tool that fetches real information, research notes are generated text - and a writer downstream cannot tell.

Passing it on makes errors sound surer

  • We gave the writer five notes, one of them false: local LLMs are "encrypted by default, so a stolen laptop cannot expose your conversations".
  • In 3 runs out of 3, the article repeated it.
  • And it improved it: "designed with security in mind", "an added layer of protection".
  • That is the risk of chaining agents. Each stage trusts the one before, and a writer’s job is to make text read well. A shaky note goes in; a polished, confident paragraph comes out.
One false note in, one confident paragraph out
Notes passed to writer_agent - the third is false: - Prompts and documents never leave your own machine. - No third-party provider can log or train on your data. - Local LLMs are encrypted by default, so a stolen laptop cannot expose your conversations. - You can run them without an internet connection. - Sensitive data can stay inside your organisation's network. Three runs, false claim repeated in all three: "Local LLMs are designed with security in mind. By default, they're encrypted, which means that even if your laptop is stolen, your conversations remain protected." "Another significant benefit of local LLMs is their default encryption." "Local LLMs are also encrypted by default, providing an added layer of protection."

A reviewer agent is not a fact-checker

The obvious fix is a third agent - the proofreader - told to list every claim the notes do not support:

  • It approved the article with the false encryption claim. Correctly, in its own terms: the claim IS in the notes.
  • It also approved the article where the writer added claims of its own - twice: "None. Every claim in the article is supported by the research notes."
  • Lesson 1: a reviewer that sees only the notes can check that the article MATCHES them - never that they are TRUE.
  • Lesson 2: with a small model, "review this" can turn into a rubber stamp.
  • Real checking needs real sources - POC 3 gives the researcher a search tool - and, for anything that matters, a person.
A third agent: the reviewer
def reviewer_agent(research_notes, article): response = ollama.chat( model="llama3.1", messages=[ {"role": "system", "content": ( "You are a fact-checking reviewer. Compare the article with the research notes. " "List every claim in the article that the notes do not support, one per line. " "If every claim is supported, reply with only the word APPROVED." )}, {"role": "user", "content": f"Research notes:\n{research_notes}\n\nArticle:\n{article}"}, ], options={"temperature": 0}, ) return response["message"]["content"] review = reviewer_agent(notes, article) print(review)
What the reviewer said
On the article with the false encryption claim: * None. Every claim in the article is supported by the research notes. -> true: the false claim IS in the notes. It checks consistency, not truth. On the article with the writer's own additions ("third-party services", "win-win"): * None. Every claim in the article is supported by the research notes. -> missed them, twice.

Two agents at a glance

Agent

A function: a role (system message) plus one model call.

def researcher_agent(topic): ...
Hand-off

Pass one agent’s output into the next agent’s prompt.

writer_agent(topic, notes)
Shared memory

None - only the text you pass.

f"Research notes:\n{research_notes}"
Loop

None - a fixed sequence of calls.

notes -> article
Grounding

Real facts need a real source: a tool.

search(query)  (POC 3)

Try it yourself

The code does not change. Swap the content string and the program does something else entirely.

Another topic

“Benefits of microservices architecture”

A checkable topic

“The history of the Python programming language”

Plant a false note

“Add one wrong fact to the notes by hand and see whether the article repeats it.”

Add a reviewer

“Chain reviewer_agent(notes, article) on the end and see what it approves.”

What usually goes wrong

Expecting the agents to share memory

Each is a separate call. The writer knows only what you put in its prompt.

✗ writer_agent(topic)  # hoping it remembers the notes
✓ writer_agent(topic, notes)
Calling a tool-less agent a researcher

Its notes are generated, not looked up - confident, and sometimes wrong. Give it a real source.

Trusting the last agent in the chain

Each stage inherits the errors of the one before, and a writer makes them read better. Check the notes, not just the article.

Treating a reviewer agent as a fact-checker

A reviewer that sees only the notes can confirm consistency, never truth - and a small model may approve everything.

Calling a fixed hand-off an autonomous agent

Two calls in a row cannot change course, ask for more, or decide they are done. That needs a loop (Lesson 1.6) or a graph (Module 3).

Key points

  • A multi-agent pipeline can be two plain functions: notes = researcher(topic); article = writer(topic, notes).
  • Each agent’s role is its system message.
  • Agents share nothing but the text you pass between them.
  • A hand-off is a fixed sequence; ReAct is a loop that chooses its next step.
  • A researcher without tools generates facts - in our runs, some were wrong.
  • A writer repeats and polishes whatever it is given - a false note came out stronger, 3 times of 3.
  • A reviewer checks consistency with the notes, not truth - real verification needs real sources.

Quick check before you move on

What is handed from the researcher to the writer?
The researcher’s output text - the notes - pasted into the writer’s prompt.
Do the two agents share conversation history?
No. Each is its own call with its own system message.
Does the researcher actually research anything?
No - it has no search tool, so its facts come from the model’s training.
Is there a loop?
No. It is two fixed calls in sequence.
Why did the reviewer approve an article with a false claim?
The false claim was in the notes, and the reviewer only checks the article against the notes.

Quiz

  1. 1.

    What is being "handed off" from the researcher agent to the writer agent?

  2. 2.

    Do the two agents share the same conversation history?

  3. 3.

    How is this different from the ReAct loop in Lesson 1.6?

  4. 4.

    The notes contain a wrong fact. What does the writer do with it?

  5. 5.

    What would make the researcher’s notes trustworthy?

Interview questions

What is a multi-agent system?

Several agents with specialised responsibilities that cooperate by passing information between them. The simplest form is a pipeline where one agent’s output becomes the next agent’s input.

Do you need a framework to build multiple agents?

No. Ordinary functions and model calls are enough for a pipeline. Frameworks add shared state, routing, loops between agents, and observability on top of the same data flow.

Do agents share memory?

Not unless you build it. Each call sees only its own messages, so anything one agent needs from another has to be passed explicitly.

What are the risks of chaining agents?

Errors propagate and get amplified: an ungrounded fact from one agent is repeated, often more confidently, by the next. Reviewer agents that see only upstream output check consistency, not truth. Ground agents in real sources and keep a human check for anything that matters.

Comments

Sign in to leave a comment. Your name and photo come from Google; nothing else is shared.

Loading comments...