HB hakanbogan.com
hakanbogan/react-agent-calculator

ReAct Agent

A minimal implementation of the ReAct pattern, where the model alternates between reasoning about what it needs and acting through a tool. Each observation it gets back decides the next step.

The agent gets a single tool, and all it does is count the characters in a string. I chose something that dull on purpose. A language model will get that count wrong often enough to matter, but it is perfectly capable of deciding to hand the job to a tool.

That keeps the two halves of the loop apart. The reasoning is easy to follow. The tool call has to happen, because the count cannot come from the model itself. Had the task been something the model might have muddled through on its own, nothing in the trace would show which part of the answer needed the tool.

Running it prints the loop as it happens. A thought comes first, then the call to the counting tool, then the number it hands back, then the next thought, and round again until the model has what it needs. Then it answers.

I built it with LangChain against OpenAI models. The agent, the tools and the helpers live in separate modules, with a test over each. I wrote it to be read rather than deployed, and never changed that. It is what I open when someone asks how the control flow works.

## stack
  • Python
  • LangChain
  • OpenAI
  • pytest
→ github.com/hakanbogan/react-agent-calculator