Building a Multi-Tool Research Copilot: A Step-by-Step Agentic AI Tutorial Using Real-World Web APIs

Key Takeaways
- Standard chatbots can only talk; AI agents can act by using external tools like APIs to access live data and perform tasks.
- Agents operate on a ReAct (Reason + Act) loop: the AI thinks about a problem, chooses a tool, executes an action, and observes the result to inform its next step.
- This guide walks you through building a multi-tool research copilot in Python that can combine web search, weather data, and financial news to answer complex questions.
A developer recently asked a standard chatbot, "What’s the current stock price of the company that made the best-selling video game last month?" The bot failed completely because it couldn't access real-time data, determine the best-selling game, or connect the two.
The reason is simple: it was designed to talk, not to act. That single failure is the entire reason we need to move beyond simple chatbots and start building real AI agents.
Introduction: Beyond Chatbots to Autonomous Agents
The Limitations of Standard LLMs
Large Language Models (LLMs) like GPT-4 are incredible feats of engineering, trained on a huge slice of the internet. But that knowledge has a fatal flaw: it's static.
An LLM is like a brilliant historian locked in a library with books that are already a year or two out of date. It can't check the weather, look up a stock price, or query your company's internal database because it's trapped in its own training data.
What is an 'Agentic' AI?
This is where the paradigm shift happens. An "agentic" AI is an LLM given hands and eyes. Instead of just generating text, it can reason about a problem, formulate a plan, and then use tools to execute that plan.
These tools are just functions connected to real-world APIs. The agent can decide which tool to use, what data to pass to it, and how to interpret the result, all on its own. It's a system that can autonomously achieve a goal.
Project Goal: Our Multi-Tool Research Copilot
Today, we're going to build one of these systems from the ground up. Our goal is to create a research copilot that can answer complex questions by intelligently combining information from multiple live web APIs. It will be able to search the web, check the weather, and pull financial news in a single, coordinated workflow.
Prerequisites & Environment Setup
What You'll Need (Python, IDE, basic API knowledge)
I'm assuming you have a basic grasp of Python and know your way around an IDE like VS Code. You don't need to be a grizzled veteran, but you should be comfortable writing functions and installing packages. You’ll also need to know what an API key is and how to get one.
Initializing Your Project and Installing Dependencies
First, create a new project folder, set up a virtual environment, and install the necessary libraries. We'll be using LangChain as our agentic framework because it provides excellent building blocks for this kind of work.
pip install langchain langchain-openai python-dotenv tavily-python
We'll also need a .env file in our root directory to store our API keys securely.
Gathering Your API Keys (OpenAI, Search API, Weather API)
You're going to need a few keys to give our agent its powers:
- OpenAI API Key: For accessing the LLM brain (GPT-3.5 or GPT-4).
- Tavily AI API Key: For our real-time web search tool.
- OpenWeatherMap API Key: For fetching live weather data.
Create your .env file and add your keys like this:
OPENAI_API_KEY="sk-..."
TAVILY_API_KEY="tvly-..."
OPENWEATHERMAP_API_KEY="..."
The Core Architecture: Building the Agent's Brain
Choosing an Agentic Framework (e.g., LangChain, LlamaIndex, or from scratch)
While you could build the entire agent loop from scratch, frameworks like LangChain or LlamaIndex handle the complex plumbing of prompt formatting, tool dispatching, and state management. I'm a big fan of LangChain for this because its "LangChain Expression Language" (LCEL) makes chaining components together incredibly intuitive.
Understanding the ReAct (Reason + Act) Prompting Loop
The magic behind most modern agents is a concept called ReAct (Reason + Act). It's a simple but powerful loop:
- Reason: The LLM looks at the user's query and available tools. It "thinks" out loud about what it needs to do next.
- Act: The LLM outputs a structured call to a specific tool with the required parameters.
- Observe: Our code executes the tool call and gets a result, which is fed back into the loop as an "observation."
The agent repeats this process until it has gathered enough information to answer the original query.
Defining the Main Agent Executor
The Agent Executor is the runtime that actually runs the ReAct loop. It orchestrates the flow between the LLM, our tool definitions, and the user's input. We'll define our tools first and then pass them to the executor to create our final, runnable agent.
Step 1: Creating the Web Search Tool
Selecting and Integrating a Real-Time Search API (e.g., Tavily, Serper)
A generic LLM is blind to current events. A search tool is its eyes on the world. I'm using Tavily here because it's designed to return clean, agent-friendly search results.
Writing the Function Wrapper for the Tool
In LangChain, we define tools as decorated functions. The function's docstring is crucial—it's how the LLM knows what the tool does and when to use it.
from langchain_community.tools.tavily_search import TavilySearchResults
# Tavily provides a pre-built tool wrapper
search_tool = TavilySearchResults(max_results=2)
search_tool.description = "A search engine useful for when you need to answer questions about current events, data, or things you don't know. Input should be a search query."
Testing the Agent with a Single Tool
With just one tool, we can already build a basic agent. We'll bind the tool to our LLM and create an executor. Now, it can use the search tool to answer questions about recent events.
Step 2: Adding a Second Tool - The Weather API
Connecting to a Service like OpenWeatherMap
Now let's give our agent another sense by connecting to OpenWeatherMap to get live weather data. This requires making a direct HTTP request.
Building the 'get_weather' Tool
This time, we'll write the tool function ourselves.
import requests
import json
from langchain.tools import tool
@tool
def get_weather(city: str) -> str:
"""Gets the current weather for a specified city."""
API_KEY = os.getenv("OPENWEATHERMAP_API_KEY")
base_url = "http://api.openweathermap.org/data/2.5/weather"
params = {"q": city, "appid": API_KEY, "units": "metric"}
response = requests.get(base_url, params=params)
data = response.json()
return json.dumps(data)
Notice the docstring and the type hint (city: str). This is the schema the LLM uses to understand the tool's purpose and its required inputs.
Teaching the Agent to Choose Between Two Tools
Now, we give our agent a list containing both the search_tool and the get_weather tool. Based on the user's query, the LLM will now decide which tool is more appropriate. The agent is already showing signs of intelligence!
Step 3: Integrating a Financial Data API
Using an API for Stock Prices or Company News (e.g., Alpha Vantage)
Let's complete our research toolkit with a financial API. We'll pretend we have a tool that can fetch the latest news for a given company ticker symbol.
Implementing the 'get_financial_data' Tool
The pattern is the same. We define a function with a clear docstring and input schema.
@tool
def get_financial_data(company_name: str) -> str:
"""Fetches recent financial news and stock data for a given company name."""
# In a real implementation, this would call Alpha Vantage or another API.
# We will simulate the output for this tutorial.
print(f"--- Fetching financial data for {company_name} ---")
if "Apple" in company_name:
return "Recent News: Apple announced a new line of AI-powered devices. Stock is up 3%."
else:
return f"No specific news found for {company_name}."
Finalizing the Multi-Tool Toolkit
Now we assemble our full toolkit: a list of all three tools (search_tool, get_weather, get_financial_data). This list is the agent's entire world of capabilities.
Putting It All Together: Running a Complex Research Query
How the Agent Selects the Right Tool for the Job
The LLM makes its decision by comparing the user's query against the descriptions of every tool in its toolkit. This is why writing clear, descriptive docstrings is the single most important part of building a reliable agent.
Live Demo: "What is the latest news about the company headquartered in the city where it is currently raining?"
This query is impossible for a standard chatbot, but our agent can tackle it. It requires multi-step reasoning and the use of multiple tools.
Analyzing the Agent's Thought Process and Actions
When we run this query, we can watch the agent's "thoughts" in the console. It will look something like this:
- Thought: The user is asking about a company in a city where it's raining. I need to find a major tech hub and check the weather there. I'll start with "Seattle".
- Action:
get_weather(city="Seattle") - Observation: Weather data shows "rain".
- Thought: Okay, it's raining in Seattle. Now I need to find a major company headquartered in Seattle. I'll use the search tool.
- Action:
search_tool(query="major tech company headquartered in Seattle") - Observation: Search results mention "Amazon" and "Microsoft". I'll pick Amazon.
- Thought: Now I need to find the latest news for Amazon. I should use the financial data tool.
- Action:
get_financial_data(company_name="Amazon") - Observation: Fetched recent news about Amazon.
- Thought: I have all the pieces. I can now form the final answer.
- Final Answer: "It is currently raining in Seattle. Amazon, which is headquartered there, recently announced..."
This is true agentic behavior in action.
Conclusion and Next Steps
Recap: You've Built a True AI Copilot
Congratulations! You've gone beyond a simple chatbot and built a genuine research copilot. It can reason, plan, and execute actions using live data from the web. This is a fundamental building block for the next generation of AI applications.
How to Add More Tools
Adding more capabilities is as simple as defining more functions with the @tool decorator and adding them to the agent's tool list. You could connect to a CRM, a calendar API, or a database—anything with an API can become a tool in your agent's belt.
Ideas for Improvement: Error Handling and UI Integration
For a production system, you'd need robust error handling, retry logic, and a way to manage state more effectively. For truly complex workflows, you might need multiple agents collaborating, which requires more advanced orchestration frameworks.
Go ahead and try building your own agent. The possibilities are endless.
Recommended Watch
π¬ Thoughts? Share in the comments below!
Comments
Post a Comment