I want to understand the internals of agentic AI by building my own harness from scratch in Python rather than relying on heavy frameworks. I want to learn how the loop works under the hood.
Does anyone have a tutorial or a minimal implementation example? I am interested in seeing how to build the observation-thought-action loop without using too many external dependencies. Any pointers on handling token limits during the reasoning loop would also be great.
An agentic harness is constructed by implementing a loop that continuously passes an LLM's structured output through a function executor and re-injects the resulting observation into the conversation history until a final state is reached.
5 answers
Building a minimal agentic harness requires a standard execution flow to manage the cycle between thought and action.
- Initialize your message history with a system prompt defining the tool availability.
- Enter a loop that prompts the model to generate a structured output like JSON.
- Parse that output to trigger your specific tool or function.
- Append the tool result as a new observation message to the history.
- Repeat until the model returns a final answer token.
You absolutely can build a harness from scratch, and honestly, it is the only way to truly understand the failure modes of agentic loops. Start by implementing a simple while-loop that passes the output of an LLM call to a function executor, then feeds the result back into the prompt as a new message history.
I recall trying to automate cross-chain asset verification back in 2021, and the initial temptation was to pull in a massive orchestration library. I ended up stripping everything back to raw requests and a basic state machine because the black-box abstraction of those frameworks made debugging impossible when the entropy of the chain hit the agent logic.
You are on the right track by avoiding the bloat. By writing your own loop, you start to see the state transitions not as magic, but as simple sequential data transformations.
When you build your own loop, you have to decide whether to prioritize state transparency or latency. Hard-coding the message truncation logic based on token counts works well for simple prototypes, but it fails to account for the semantic importance of the system prompt versus older observations. If you use a sliding window approach, you might drop context that the agent needs to prevent loops, whereas a summarization approach adds overhead but preserves the intent of the conversation. In enterprise deployments, the choice between these two often determines the success of long-running tasks, as simple truncation often leads to agent amnesia during complex workflows.
Nisha Patil, your point about agent amnesia is really hitting home. I've been struggling with these workflows and feel a bit overwhelmed, but this perspective makes the trade-offs feel much more manageable.
I appreciate your insight, Nisha Patil. I have been leaning toward truncation, but seeing it framed as a choice between overhead and context helps me rethink my approach to these loops.
Frameworks are just wrappers for API calls you can write yourself in ten lines of Python. Stop overthinking the architecture and just implement a basic recursive function that checks the token count of your context list before every single call to the LLM. If the count exceeds your limit, truncate the oldest observation messages while keeping the system prompt intact. This is the only way to ensure the agent doesn't hallucinate due to context loss, and frankly, it is the most robust method for any mission-critical automation.


Thanks for the clear steps, Abigail Ruiz. I tend to overcomplicate my harness logic, so seeing such a straightforward loop structure is really helpful for my current project. I should simplify things.