Implementing a Research Agent
Most of today’s agents use a basic ReAct (Reason, Act, Observe) loop. The agent first reasons, then acts (calls a tool) and observes the tool result. This loop runs until there are no more tool calls and the model returns its final answer. The next user message restarts the loop. As the conversation continues, the context gets filled with the model’s reasoning and tool call results. Frontier LLMs are trained with long context in mind and can follow the initial instructions for the whole conversation. This makes them suitable for long tasks such as deep research. Smaller models, on the other hand, can’t yet do that.
With growing context, smaller models’ instructions get buried and they forget to follow them, hallucinate tool call results and lose track of the goal. I run small models in my homelab and I want to get the most out of them. That requires creative solutions to these problems.
Other Agent Types
There are other types of agents that try to work around these limitations. For example - a Plan and Execute agent. It uses two agents, a planner and an executor. The planner gets the user query and a list of tools that exist and creates a step-by-step plan. The executor then executes a single step, calling the exact tools it needs. The tool results go back to the planner, which then replans future steps based on the observations. Another type of agent is a State Machine agent. The agent might return structured output with attributes like state, context and response. On the next conversation turn, the agent gets the context from the previous turn and the user query. In both of these cases, the agent only has the information it needs, even when we are several turns deep. The context is bounded and does not grow with more turns.
Workout Agent
To illustrate this, I tried to build a workout logging agent. I’d say something like “Hack squat, 80kg, 3x8 reps” and it was supposed to call the log_exercise tool. For the first few sets of the workout, it worked like a charm. Then, as more and more tool call results were in the context, the model started to hallucinate tool call results without actually calling the tool. It would claim that the set was logged, but it never was. To work around this limitation, I created a Structured agent. I realized that the agent doesn’t need to know that I did dumbbell bench press 6 sets earlier. It only needs to know the current status (what was logged so far) and my current message (what I just did). The Structured agent gets the instructions, the current context and the user message. It calls the log_exercise tool and returns JSON with the next context and the response for the user. It’s essentially always the first set for the model, when the tool calling is most reliable.
Research Agent
With the workout agent working, my next agent to implement was the research agent. My initial idea was to have a Structured agent outer loop and a ReAct agent inner loop. The outer loop agent would have a research plan with subtasks. Each subtask would be researched by the inner agent. After implementing it I realized the outer loop can be completely deterministic, and after iterating the design I landed on several cascading agents. First a Planner agent one-shots a research plan with subtopics and subquestions to research. Then a Researcher agent takes one unfinished question from the research plan, researches it and writes findings into a findings file. It should then mark the subtask as finished. The Replanner agent runs after the Researcher agent, reads its findings file and replans the rest of the research plan, so that we don’t end up researching a debunked idea or going down a dead end. Finally, once all tasks from the research plan are done, the Summarizer agent reads all findings files and creates REPORT.md.
During testing, I encountered a few issues. Sometimes, the agent forgot to write the findings file and update the research plan. It would only print the findings into the chat and not use the tool. I implemented a simple nudge. It checks if the file was written and, if not, it nudges the agent with another message, like a human would in a normal chat app. Another problem was that web_fetch could return too many tokens and overwhelm the context. I implemented a limit on how many tokens web_fetch could return and added a website summarizer subagent. The final issue was that all findings files together could still overwhelm the model’s context. To solve this, I implemented summarization in stages.
Running It Locally
Initially I ran the Research agent on a Qwen3.6-35B-A3B mixture-of-experts model on my mac mini in my homelab. More recently, I switched to the Qwen3.8-27B dense model. Due to the mac mini’s low memory bandwidth, it is quite slow. The context juggling means that the prefix cache is often invalidated and we have to pay the full price of prompt processing. A single research task can easily take over an hour to complete, while a frontier LLM would take just minutes. But it’s all running locally and I was blown away by the quality such a small model produced. What makes small models work with long tasks is the architecture around them. Every subagent sees only a small, bounded prompt and nothing gets buried in the long context.