A practical guide to build an Langchain AI Agent that uses its own tools

Abhinav Krishna K S Avatar

A chatbot answers from what it learned in training. An agent can go further: it decides when it needs outside help, uses a tool, reads the result, and carries on. For my Generative AI internship at Valentius Kryptix, I built a small LangChain agent to see this step from chatbot to agent for myself. This post covers what I built, what broke, and what I learned.

What I built

The agent uses LangChain’s create_agent, which is built on LangGraph, with Google’s Gemini model gemini-3.5-flash-lite. It has three tools, each doing something a language model can’t do reliably alone:

  • Calculator: exact arithmetic using Python’s ast module, with no eval. Models often slip on large-number math.
  • Web search: live results through the ddgs package, for information that changes after the model’s training date.
  • Python REPL: runs real code in a subprocess with a 10-second timeout.

Every step is logged as [USER], [THOUGHT], [TOOL CALL], [TOOL RESULT] and [FINAL ANSWER], so you can see why the agent acted and what it got back.

What the tests showed

I ran five cases and saved the full output in the repository.

  1. No tool needed: “What is the capital of France?” got a direct answer with no tool call.
  2. Single tool: a large calculation went to the calculator once and returned 1267650600228351334127815840645.
  3. Multi-step: I asked it to find India’s population and compute people per square kilometre. It searched, found 1,467,231,210, passed that number to the calculator, and answered about 446.
  4. Calculator failure: dividing by zero returned an error string, and the agent reported it.
  5. Search failure: I forced the search tool to raise a network error. The agent reported the failure, said its fallback answer might be out of date, and did not crash.

What went wrong along the way

The first version looked fine until I ran the multiplication test. The calculator returned the right answer, and then the agent re-checked it four more times with other tools until it hit the recursion limit. A smaller model like flash-lite kept doubting a correct result.

I made three changes:

  1. Clearer tool descriptions. The calculator now says its result is final and exact. The Python tool says not to use it for simple arithmetic.
  2. Code-level guards. Prompts alone weren’t enough, so the run loop now stops repeated identical calls, calls to the Python tool after a successful calculation, more than three calls to one tool, two errors in a row from one tool, and more than five calls in total.
  3. Tool wrappers that never raise. Each tool catches its own exceptions and returns text starting with “Error:”, so the agent can read the failure and respond.

One more bug taught me something. When a guard forced a final answer, the model once made up a number that was off by a few digits. The fix was to pass the latest successful tool result straight into the forcing message and tell the model to use those exact digits.

Key lessons

Tool descriptions are part of your prompt. The agent picks tools from their descriptions, so overlapping or vague ones cause wrong or repeated calls.

Guardrails belong in code. You can ask a model to behave in a prompt, but a loop limit or error-streak check is what keeps an agent from burning through quota.

Failures should be data. Returning errors as text, instead of raising exceptions, lets the agent handle problems like any other result.

Check the final answer, not just the tool output. A correct tool result can still be reworded wrongly by the model, so I compared digits by hand.

Limitations

The agent has no memory between questions, and its fallback answers come from the model’s own knowledge when tools fail. The search backend also returns loosely matched results for nonsense queries, so an empty result doesn’t always mean nothing was found.

Try it yourself

The full code, setup steps and logged runs are on GitHub: https://github.com/KRISH18833/langchain-agent

Next time I’d add conversation memory and a small web interface. For now, the main takeaway is that an agent is only as reliable as the guardrails around it.

Tagged in :

Abhinav Krishna K S Avatar

Leave a Reply

You May Love