SMJ
HomeAboutExperienceStackProjectsContactBlog
ResumeAdmin

Shashwat Manglam Jain

Freelance full-stack developer helping teams ship web + AI products.

HomeAboutExperienceStackProjectsContact
GitHubLinkedInXInstagramWhatsAppResume

© 2026 Shashwat Manglam Jain. All rights reserved.

Built with Next.js, Tailwind, NestJS, and Prisma.

← Back to Blog

Building My First AI Agent from Scratch

June 22, 2025•3 min read
AILLMAgentsPython
Building My First AI Agent from Scratch

Building My First AI Agent from Scratch

The hype around AI agents is loud. But I wanted to understand what an agent actually is under the hood, so I built one from scratch. Not with a framework, not with a drag-and-drop builder — just Python, an LLM API, and a loop.

What Is an Agent, Really?

Strip away the marketing and an AI agent is a program that: takes a goal, decides what tool to use, executes the tool, observes the result, and decides what to do next. It is a loop with an LLM as the decision engine.

The simplest version looks like this:

while not done:
    thought = llm.think(goal, observations)
    action = llm.pick_tool(thought, available_tools)
    result = execute(action)
    observations.append(result)
    done = llm.is_goal_met(observations)

That is it. Everything else is optimization and guardrails.

The Tools I Gave It

I built a research agent that could answer questions by searching the web and reading pages. The tools were:

  • web_search: Takes a query, returns a list of URLs and snippets.
  • read_page: Takes a URL, returns the text content of the page.
  • answer: Takes a final answer and ends the loop.

The agent decides which tool to call based on what it knows so far. If the search results are not sufficient, it reads specific pages. If it has enough information, it synthesizes an answer.

What Surprised Me

Token management is the real engineering. The agent's context window fills up fast. After three tool calls, I was hitting token limits. I had to implement summarization of previous observations and careful pruning of irrelevant content. This is where most of the engineering effort went.

Prompt engineering is architecture. The system prompt defines the agent's personality, reasoning style, and tool-use conventions. A small change in the prompt can make the agent loop forever or give up too early. I iterated on the prompt more than the code.

Error handling is critical. What happens when a web search returns no results? When a page cannot be read? When the LLM hallucinates a tool name that does not exist? Each failure mode needs a graceful recovery path.

What I Would Do Differently

  1. Add a planning step. Before diving into tool calls, the agent should outline a plan. This reduces unnecessary searches and makes the reasoning more coherent.
  2. Implement memory. My agent forgot everything between sessions. A simple vector store for past research would make it far more useful.
  3. Use structured outputs. Parsing free-text tool calls is fragile. JSON mode or function calling from the LLM API makes tool selection much more reliable.

The Takeaway

Building an agent taught me more about LLMs than any tutorial. You learn the limits quickly: context windows, latency, cost per call, hallucination rates. You also learn that the "intelligence" of an agent is mostly in the tooling and the loop design, not just the model.

If you want to understand AI agents, build one. Start with a simple loop and two tools. The complexity will find you soon enough.

Enjoyed this post?

I'm always happy to connect and discuss ideas.

Contact meView projects