Building Custom Tools and Agents with LangChain (gpt-3.5-turbo)
Based on Sam Witteveen's video on YouTube. If you like this content, support the original creators by watching, liking and subscribing to their content.
Custom tools in LangChain are callable functions wrapped with a name, description, and input hint so a ReAct-style agent can choose when to use them.
Briefing
Custom tools are the key lever for making LangChain conversational agents more useful—and the biggest practical lesson is that tool use often requires careful prompt and tool-description tuning, especially with chat-based models like gpt-3.5-turbo. The walkthrough starts with the simplest possible tools: a “meaning of life” function that returns a fixed string (42.17658) and a “random number” function that returns a random value. Both are wrapped as LangChain tools so a ReAct-style agent can decide when to call them, based on the tool name, description, and expected input.
When the agent is asked “What is the time in London?”, it correctly selects an external search tool (DuckDuckGo in the earlier context) and returns search results. Asking for “a random number” also works as expected: the agent chooses the random-number tool, receives the output (e.g., 4), and then responds in a chatty style that includes the tool result in its message history. But the “meaning of life” tool reveals a core failure mode: even when a tool exists and the question matches it, the chat model may still answer directly instead of calling the tool. In this run, the agent produced a philosophical explanation rather than returning the tool’s exact value.
The fix is not changing the tool logic—it’s changing the instructions. The transcript shows prompt engineering as a requirement: the system prompt is overwritten to explicitly tell the assistant it “does[es]n’t know” about meaning-of-life answers and must use the meaning-of-life tool for those topics. After that adjustment, the agent reliably calls the tool, uses the action input (“MOL”), and returns the tool’s full string. That also surfaces another nuance: the tool output includes more than the user asked for (it returns both the rounded and unrounded value), and the model may not automatically trim it to match the user’s wording. The takeaway is that tool descriptions and prompts must be precise about both when to call tools and what level of formatting the tool should return.
Next comes a more practical custom tool: a webpage “page getter” that fetches HTML, strips tags using BeautifulSoup, and truncates output to avoid context limits (the transcript notes a ~4,000+ token input constraint). The tool also acknowledges messy real-world text extraction—like excessive newlines—and suggests filtering them. Implemented either as a simple function tool or as a LangChain BaseTool subclass with a run method, it returns cleaned text to the agent.
With the updated prompt—now also instructing the assistant to use the webpage tool when asked about web content—the agent can answer questions like “Is there an article about Clubhouse on TechCrunch today?” and “What are the titles of the top stories on CBSnews.com.” In both cases, the agent fetches the relevant page (sometimes via an intermediate search step), extracts enough text to identify the requested items, and returns titles and dates. The overall message is clear: custom tools make agents dynamic, but reliable tool use depends on aligning prompts, tool descriptions, and output formatting with the model’s chat behavior and context constraints.
Cornell Notes
LangChain custom tools are implemented as callable functions that a ReAct-style agent selects based on tool names, descriptions, and prompts. Simple tools (like a fixed “meaning of life” and a “random number” generator) demonstrate that tool calling can work reliably for some requests but fail for others—especially when gpt-3.5-turbo answers directly instead of using the tool. The transcript shows that overwriting the system prompt to explicitly say the assistant lacks knowledge about certain topics and must use specific tools can force correct tool usage. For real utility, a custom webpage tool strips HTML with BeautifulSoup and truncates content to fit context limits, enabling the agent to answer questions about current articles and top stories on sites like TechCrunch and CBS News.
How are custom tools structured in LangChain for a tool-using agent?
Why did the “meaning of life” tool fail at first, even though the question matched it?
What change made the agent reliably call the “meaning of life” tool?
What practical constraints shape custom tool design for web content?
How does the webpage tool enable answers about current articles and headlines?
Review Questions
- What specific prompt instruction was added to force the agent to call the meaning-of-life tool, and what was the observed effect?
- Describe how the webpage getter tool handles both HTML parsing and context-length constraints.
- Give one example of a tool-calling success and one example of a tool-calling failure from the transcript, and explain the difference in outcome.
Key Points
- 1
Custom tools in LangChain are callable functions wrapped with a name, description, and input hint so a ReAct-style agent can choose when to use them.
- 2
Chat-based models may answer directly instead of calling an available tool, even when the user’s question matches the tool’s purpose.
- 3
Overwriting the system prompt to explicitly state what the assistant does not know—and which tool must be used for certain topics—can significantly improve tool reliability.
- 4
Tool output formatting matters: if a tool returns extra details (like both rounded and unrounded values), the model may not automatically trim to the user’s exact request.
- 5
For web-based tools, stripping HTML and truncating content are essential to avoid context limits and to keep extracted text usable.
- 6
Implementing tools as BaseTool subclasses (with run/async run) provides a structured way to reuse logic like webpage fetching and cleaning.
- 7
Dynamic agents become practical when tools fetch targeted external information (web pages, docs, or APIs) and return cleaned, bounded text for downstream reasoning.