Building an AI agent from scratch, one stage at a time.
View the Project on GitHub the-ai-coffee/ai-agent-from-scratch
by mikamboo
| 🇬🇧 English | 🇫🇷 Français |
Every agent, no matter how capable, is built around the same shape: read an observation, decide on an action, repeat. Before adding an LLM, tools, or evals, it’s worth building that loop on its own so later stages have something concrete to extend.
agents/agent-001-echo-loop/agent.py
implements the loop with no dependencies:
def run(input_stream, output_stream):
while True:
output_stream.write("User> ")
output_stream.flush()
line = input_stream.readline()
if not line:
break
line = line.rstrip("\n")
if not line:
break
output_stream.write(f"Agent> {line}\n")
input_stream.output_stream.while loop, which continues until EOF or an empty line.Passing in input_stream and output_stream (rather than hardcoding
sys.stdin/sys.stdout) is what makes this testable without spawning a
process – the tests in test_agent.py just pass io.StringIO objects.
python agents/agent-001-echo-loop/agent.py
Type a line, press enter, see it echoed. Press enter on an empty line to stop.
Agent-002 replaces the echo with a real LLM call, keeping the same read-act-repeat shape.
tags: ai - agents - from-scratch - python - agent-loop - testing