New Technologies

Can I build my own agentic harness in Python?

MI Asked by Miguel Gilbert · 09-10-2026
▲ 5 upvotes 145 views 0 comments
The question

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.

Verified summary

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

▲ 9
AB
Abigail Ruiz Accepted
Answered on 09-10-2026

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.
GA 09-10-2026

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.

▲ 10
CA
Answered on 09-10-2026

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.

▲ 0
MA
Answered on 09-10-2026

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.

▲ 8
NI
Answered on 09-10-2026

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.

KA 09-10-2026

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.

OM 09-10-2026

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.

▲ 9
DA
Answered on 09-10-2026

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.

Share your thoughts

Your email address will not be published. Required fields are marked (*)

Still have questions? Schedule a free counselling session

Our experts are ready to help you with any questions about courses, admissions, or career paths. Get personalized guidance from industry professionals.

Book Free Session

Book Free Session