Logo

dev-resources.site

for different kinds of informations.

How to Build Smarter AI Agents with Dynamic Tooling

Published at
12/12/2024
Categories
langchain
fastapi
llm
Author
louis-sanna
Categories
3 categories in total
langchain
open
fastapi
open
llm
open
Author
11 person written this
louis-sanna
open
How to Build Smarter AI Agents with Dynamic Tooling

Introduction

Imagine having an AI agent that can access real-time weather data, process complex calculations, and improve itself after making a mistake — all without human intervention. Sounds kinda neat, right? Well, it’s not as hard to build as you might think.

Large Language Models (LLMs) like GPT-4 are impressive, but they have limits. Out-of-the-box, they can't access live data or perform calculations that require real-time inputs. But with dynamic tooling, you can break these limits, allowing agents to fetch live information, make decisions, and even self-correct when things go wrong.

In this guide, we’ll walk you through how to build an AI agent that can:

  • Access real-time API data.
  • Self-correct and improve its performance.
  • Use a clean, maintainable architecture for future upgrades.

By the end, you'll have the tools you need to build an agent that's as flexible as it is powerful.


1️⃣ What are Dynamic Tools in AI Agents?

Dynamic tools allow AI agents to go beyond static responses. Instead of just generating text, agents can "call" specific actions, like fetching real-time data, executing scripts, or correcting their own mistakes.

Here’s a simple analogy:

Static AI is like a person who answers only from their memory.

Dynamic AI is like someone who can use a search engine, calculator, or dictionary to give you better answers.

Why Does This Matter?

With dynamic tools, you can build smarter AI agents that can:

  • Call external APIs for real-time data.
  • Process information like calculations, translations, or data transformation.
  • Self-correct errors in their own logic or execution.

🛠️ Example Use Case:

"What's the average temperature in Tokyo and Paris right now?"

A static AI would fail, but a dynamic AI can:

  1. Call a weather API for Tokyo.
  2. Call a weather API for Paris.
  3. Calculate the average temperature.

2️⃣ Building a Real-Time API Agent

Let’s create an AI agent that can fetch real-time weather data from an API and compute the average temperature between two cities.

Tools Required

  • FastAPI: To build the backend.
  • Asyncio: For asynchronous event handling.
  • An External Weather API: For real-time temperature data (like OpenWeatherMap or WeatherAPI).

🧑‍💻 Step 1: Setting up the Environment

Start by installing the necessary packages:

pip install fastapi uvicorn httpx

Enter fullscreen mode Exit fullscreen mode

🧑‍💻 Step 2: Writing the Agent

Here’s how the agent works:

  1. It receives a list of cities (like Tokyo and Paris).
  2. It fetches live weather data for each city using HTTPX (an async HTTP client).
  3. It calculates the average temperature and returns it.

Code Implementation

import httpx
from fastapi import FastAPI, Request

app = FastAPI()

# 🌦️ Step 1: Function to get weather data
async def get_weather(city: str) -> float:
    """Fetch the current temperature for a given city using an API."""
    API_KEY = "your_api_key_here"
    url = f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={API_KEY}&units=metric"
    async with httpx.AsyncClient() as client:
        response = await client.get(url)
        data = response.json()
        temperature = data['main']['temp']
    return temperature

# 🌦️ Step 2: Agent to calculate average temperature
@app.post("/average-temperature")
async def calculate_average_temperature(request: Request):
    """Takes a list of cities and returns the average temperature."""
    payload = await request.json()
    cities = payload.get("cities", [])

    if not cities:
        return {"error": "Please provide a list of cities"}

    temperatures = await asyncio.gather(*[get_weather(city) for city in cities])
    average_temp = sum(temperatures) / len(temperatures)

    return {"average_temperature": average_temp}

Enter fullscreen mode Exit fullscreen mode

How It Works

  1. get_weather(city): Calls the weather API to get the temperature for a city.
  2. calculate_average_temperature(request): Loops over a list of cities, fetches weather for each, and calculates the average temperature.

🔥 Test It!
Start the server:

uvicorn filename:app --reload

Enter fullscreen mode Exit fullscreen mode

Send a POST request to http://127.0.0.1:8000/average-temperature with this JSON body:

{
    "cities": ["Tokyo", "Paris"]
}

Enter fullscreen mode Exit fullscreen mode

The response will look something like this:

{
    "average_temperature": 18.5
}

Enter fullscreen mode Exit fullscreen mode

3️⃣ Building Self-Correcting Agents

What if the agent calls the API but gets a rate-limiting error or the data is incomplete? Can it self-correct? Yes, it can!

Self-correcting agents work by analyzing failures and then attempting a new approach. Here’s a simple example.


🧑‍💻 Step 1: Detecting Errors

When we call the weather API, we may receive an error. Instead of crashing, the agent should recognize the failure and retry.

Updated Code for Resilient Agent

async def get_weather(city: str) -> float:
    """Fetch the current temperature, retrying if necessary."""
    API_KEY = "your_api_key_here"
    url = f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={API_KEY}&units=metric"

    for attempt in range(3):  # Retry up to 3 times
        try:
            async with httpx.AsyncClient() as client:
                response = await client.get(url)
                if response.status_code == 200:
                    data = response.json()
                    return data['main']['temp']
        except Exception as e:
            print(f"Attempt {attempt + 1} failed: {e}")

    raise ValueError(f"Failed to get weather data for {city} after 3 attempts")

Enter fullscreen mode Exit fullscreen mode

4️⃣ How to Build a Smarter Agent

To make our agent smarter, we can:

  1. Add Self-Correction Loops: Retry API calls on failure.
  2. Use Reflection: If a calculation fails, reattempt using a new approach (e.g., switch API providers).
  3. Modularize Tools: Use dynamic tools so the agent can call functions as needed rather than using them all the time.

Here’s an enhanced design for our agent:

1️⃣ Agent Receives User Request (e.g., "What's the average temperature in Tokyo and Paris?")
2️⃣ Agent Uses Tool: Calls get_weather(city) for each city.
3️⃣ Agent Handles Errors: If a call fails, it retries with a new method.
4️⃣ Agent Returns the Result: Final response is sent to the user.

Enter fullscreen mode Exit fullscreen mode

💡 Pro Tip:

Use reflection by logging all errors and decisions the agent makes. This log can be used to self-correct on future requests.


5️⃣ Best Practices for Dynamic Tool Agents

  • Use context-aware logging: Track failures and successes so the agent can learn.
  • Limit API calls: Use caching to avoid unnecessary calls to the same data.
  • Decouple logic: Split agent logic (like get_weather) into independent "tool" functions.

6️⃣ Key Concepts Recap

  • Dynamic Tools: Functions that agents can call (like APIs, calculators, or scripts).
  • Self-Correction: Agents analyze their own failures and retry tasks.
  • Simple Code, Smart Logic: Make your agents smarter with minimal code changes (like retries).

7️⃣ Wrapping Up

Congratulations! You've built a smarter AI agent with dynamic tools. It can access weather APIs, calculate the average temperature, and retry when things go wrong. The key concepts here — API calls, self-correction, and modular tools — are foundational for building more advanced agents that handle any user request.

Here’s a quick recap of what we did:

  • Built a weather-fetching agent.
  • Added resilience with self-correction.
  • Discussed best practices for agent development.

Want to take it to the next level? Here’s a challenge:

Build an AI agent that can pull data from multiple sources, merge it, and generate insights.

Want to learn more about building Responsive LLMs? Check out my course on newline: Responsive LLM Applications with Server-Sent Events

I cover :

  • How to design systems for AI applications
  • How to stream the answer of a Large Language Model
  • Differences between Server-Sent Events and WebSockets
  • Importance of real-time for GenAI UI
  • How asynchronous programming in Python works
  • How to integrate LangChain with FastAPI
  • What problems Retrieval Augmented Generation can solve
  • How to create an AI agent ... and much more.

Worth checking out if you want to build your own LLM applications.

🚀 Now it’s your turn to build smarter agents. Happy coding! 🚀

fastapi Article's
30 articles in total
Favicon
Protect Your APIs from Abuse with FastAPI and Redis
Favicon
Making a Todo API with FastAPI and MongoDB
Favicon
The Core of FastAPI: A Deep Dive into Starlette 🌟🌟🌟
Favicon
Python FastAPI quickstart in uv
Favicon
Automating Word Document Creation with Python and FastAPI (Using python-docx-template)
Favicon
🌍💱 New Project: Currency Exchange API
Favicon
Getting Started with FastAPI
Favicon
[HANDS ON] Service Discovery in Prometheus
Favicon
Python's Unstoppable Rise, Dominating The Modern Backend Environment
Favicon
Boost Your App Performance by 10x with Redis Caching!
Favicon
Supercharge Your API Performance with Asynchronous Programming in FastAPI
Favicon
Mastering Real-Time AI: A Developer’s Guide to Building Streaming LLMs with FastAPI and Transformers
Favicon
Integrating LangChain with FastAPI for Asynchronous Streaming
Favicon
Serverless FastAPI Development: Building Player FC API on AWS
Favicon
FastAPI + Uvicorn = Blazing Speed: The Tech Behind the Hype
Favicon
Why is My Multi-Threaded API Still Slow?
Favicon
🧩 Step-by-Step EC2 Deployment: FastAPI, MongoDB, and NGINX Setup
Favicon
🚀 Building a User Management API with FastAPI and SQLite
Favicon
Simplify Authentication with FastAPI!
Favicon
Fastapi
Favicon
The Secret Behind FastAPI’s Speed
Favicon
Maximize Your FastAPI Efficiency: Blazingly Fast Implementation of Caching and Locking with py-cachify
Favicon
Understanding REST APIs: A Beginner’s Guide
Favicon
Mastering Python Async IO with FastAPI
Favicon
Is Flask Dead? Is FastAPI the Future?
Favicon
WSGI vs ASGI: The Crucial Decision Shaping Your Web App’s Future in 2025
Favicon
Fina Categorization API made publicly free
Favicon
Self-Correcting AI Agents: How to Build AI That Learns From Its Mistakes
Favicon
How to Build Smarter AI Agents with Dynamic Tooling
Favicon
🚀 Validating User Input with FastAPI: An Example with Custom Validators

Featured ones: