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 |
Agent-001 built the read-act-repeat loop with the simplest possible action: echo the line back. This stage keeps that loop completely intact and swaps in the only thing that changes between an echo and an agent – the action itself is now a call an LLM, for instance Claude AI.
agents/agent-002-llm-call/agent.py
looks almost identical to agent-001’s loop:
def run(input_stream, output_stream, client=None):
client = client or Anthropic()
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
message = client.messages.create(
model=MODEL,
max_tokens=1024,
messages=[{"role": "user", "content": line}],
)
output_stream.write(f"Agent> {message.content[0].text}\n")
input_stream.messages.create call to Claude instead of an echo.Every line is sent on its own, with no memory of earlier lines in the conversation. There’s no message history to maintain yet, so the agent treats each line as a fresh, independent prompt – the same way agent-001 treated each line as an independent echo.
client is a parameterclient defaults to a real Anthropic() instance (which reads
ANTHROPIC_API_KEY from the environment), but it can be overridden. That’s the same idea as passing in input_stream/output_stream instead of hardcoding sys.stdin/sys.stdout: it lets test_agent.py substitute a fake client that returns a canned reply, so the test suite runs without hitting the network or needing an API key.
export ANTHROPIC_API_KEY=<your key>
python agents/agent-002-llm-call/agent.py
Type a line, press enter, see Claude’s reply. Press enter on an empty line to stop.
This stage is stateless – Agent has no idea what you said on the previous line. Agent-003 starts carrying conversation history between turns, and later stages will add tools so Agent can act on more than just text.
tags: ai - agents - llm - claude - python - anthropic-sdk