Tool

  • Tools are added to Agent to add capabilities
  • Limitations
    • Recommended to have ~5-20 tools because of context window limits, cost and latency
  • Tool design patterns

Langchain

  • Langchain provides create_agent() which can create agents
  • agent.invoke() causes agent loop to run internally and handles tool calling
from random import randint
import requests
 
from langchain.agents import create_agent
from langchain_core.tools import tool
from langchain_ollama import ChatOllama
 
 
@tool
def roll_dice():
    """
    Roll dice.
    Returns:
        int: The result of rolling a six-sided die (1-6).
    """
    return randint(1, 6)
 
@tool
def search_books(query: str):
    """
    Search for books on Open Library.
    Args:
        query: The search term (book title, author, etc.)
    Returns:
        dict: Search results from Open Library API
    """
    try:
        response = requests.get(
            "https://openlibrary.org/search.json",
            params={"q": query}
        )
        response.raise_for_status()
        data = response.json()
 
        titles = [doc.get("title") for doc in data.get("docs", [])]
 
        return {"titles": titles}
    except requests.exceptions.RequestException as e:
        return {"error": str(e)}
 
 
model = ChatOllama(
    model="qwen2.5:7b",
    temperature=1
)
 
SYSTEM_PROMPT = """
You are a helpful assistant that has following capabilities:
* You can search top books based on the user query. You have tool `search_books` that can give list of book titles
* You can also roll roll dice using `roll_dice` tool and tell the user what you got
"""
 
agent = create_agent(
    model=model,
    tools=[roll_dice, search_books],
    system_prompt=SYSTEM_PROMPT
)
 
# Please search book titles for "goblet" and roll a dice. The answer you get from rolling dice you use it to get the nth book title suggestion
input_text = str(input("Human: "))
 
messages = [
    {
        "role": "user",
        "content": input_text
    }
]
 
 
result = agent.invoke({
    "messages": messages
})
 
print("AI: " + result["messages"][-1].content)

Dry Run

[
    {
        "role": "system",
        "content": "\nYou are a helpful assistant...."
    },
    {
        "role": "user",
        "content": "Please search book titles...."
    }
]
  • Response 1
{
    "role": "assistant",
    "content": "",
    "tool_calls": [
        {
            "id": "call_lnimt2wu",
            "function": {
                "index": 0,
                "name": "search_books",
                "arguments": {
                    "query": "goblet"
                }
            }
        },
        {
            "id": "call_zmltor5i",
            "function": {
                "index": 1,
                "name": "roll_dice",
                "arguments": {}
            }
        }
    ]
}
  • Request 2
[
    {
        "role": "system",
        "content": "\nYou are a helpful assistant that has following capabilities:\n* You can search top books based on the user query. You have tool `search_books` that can give list of book titles\n* You can also roll roll dice using `roll_dice` tool and tell the user what you got\n"
    },
    {
        "role": "user",
        "content": "Please search book titles for \"goblet\" and roll a dice. The answer you get from rolling dice you use it to get the nth book title suggestion"
    },
    {
        "role": "assistant",
        "tool_calls": [
            {
                "function": {
                    "name": "search_books",
                    "arguments": {
                        "query": "goblet"
                    }
                }
            },
            {
                "function": {
                    "name": "roll_dice",
                    "arguments": {}
                }
            }
        ]
    },
    {
        "role": "tool",
        "content": "{\"titles\": [\"The golden ....\"Geviʻe zahav\"]}"
    },
    {
        "role": "tool",
        "content": "3"
    }
]
  • Response 2
{
    "role": "assistant",
    "content": "Based on your search, here are some book titles ... I will get the third book title suggestion from the list: \"Selections From Harry Potter and the Goblet of Fire. Instrumental Solos\"."
}

Detailed Dry Run

  • stream is set to false in the payload for testing purpose
  • Request 1
{
    "model": "qwen2.5:7b",
    "stream": false,
    "options": {
        "temperature": 1.0
    },
    "messages": [
        {
            "role": "system",
            "content": "\nYou are a helpful assistant that has following capabilities:\n* You can search top books based on the user query. You have tool `search_books` that can give list of book titles\n* You can also roll roll dice using `roll_dice` tool and tell the user what you got\n"
        },
        {
            "role": "user",
            "content": "Please search book titles for \"goblet\" and roll a dice. The answer you get from rolling dice you use it to get the nth book title suggestion"
        }
    ],
    "tools": [
        {
            "type": "function",
            "function": {
                "name": "roll_dice",
                "description": "Roll dice.\nReturns:\n    int: The result of rolling a six-sided die (1-6).",
                "parameters": {
                    "type": "object",
                    "properties": {}
                }
            }
        },
        {
            "type": "function",
            "function": {
                "name": "search_books",
                "description": "Search for books on Open Library.\nArgs:\n    query: The search term (book title, author, etc.)\nReturns:\n    dict: Search results from Open Library API",
                "parameters": {
                    "type": "object",
                    "required": [
                        "query"
                    ],
                    "properties": {
                        "query": {
                            "type": "string"
                        }
                    }
                }
            }
        }
    ]
}
  • Response 1
{
    "model": "qwen2.5:7b",
    "created_at": "2026-06-07T16:44:29.670274Z",
    "message": {
        "role": "assistant",
        "content": "",
        "tool_calls": [
            {
                "id": "call_lnimt2wu",
                "function": {
                    "index": 0,
                    "name": "search_books",
                    "arguments": {
                        "query": "goblet"
                    }
                }
            },
            {
                "id": "call_zmltor5i",
                "function": {
                    "index": 1,
                    "name": "roll_dice",
                    "arguments": {}
                }
            }
        ]
    },
    "done": true,
    "done_reason": "stop",
    "total_duration": 1600746667,
    "load_duration": 126610125,
    "prompt_eval_count": 291,
    "prompt_eval_duration": 62269000,
    "eval_count": 38,
    "eval_duration": 1409053000
}
  • Request 2
    • calls functions internally and get the response and inject in the payload
{
    "model": "qwen2.5:7b",
    "stream": false,
    "options": {
        "temperature": 1.0
    },
    "messages": [
        {
            "role": "system",
            "content": "\nYou are a helpful assistant that has following capabilities:\n* You can search top books based on the user query. You have tool `search_books` that can give list of book titles\n* You can also roll roll dice using `roll_dice` tool and tell the user what you got\n"
        },
        {
            "role": "user",
            "content": "Please search book titles for \"goblet\" and roll a dice. The answer you get from rolling dice you use it to get the nth book title suggestion"
        },
        {
            "role": "assistant",
            "tool_calls": [
                {
                    "function": {
                        "name": "search_books",
                        "arguments": {
                            "query": "goblet"
                        }
                    }
                },
                {
                    "function": {
                        "name": "roll_dice",
                        "arguments": {}
                    }
                }
            ]
        },
        {
            "role": "tool",
            "content": "{\"titles\": [\"The golden goblet\", \"Harry Potter and the Goblet of Fire\", \"Selections From Harry Potter and the Goblet of Fire. Instrumental Solos\", \"Benjamin and the Silver Goblet\", \"The Golden Goblet\", \"Harry Potter (series) 1-7\", \"Harry Potter and the Goblet of Fire\", \"Harry Potter/Goblet of Fire Easy Piano\", \"The Golden Goblet\", \"The Stolen Goblet\", \"Pretending is lying\", \"Goblets Immortal\", \"Golden Goblet\", \"The king's goblet\", \"Cyclops Goblet\", \"\\\"Goblets II\\\" ..\", \"The Goblet\", \"Chronographie\", \"Creepers\", \"The first goblet for the fortification of faith\", \"Arthur's temptation, or, The lost goblet\", \"Harry Potter Boxed Set\", \"The silver goblet\", \"The golden goblet\", \"The Goblet Club\", \"The golden goblet\", \"The crystal goblet\", \"A Guide for Using The Golden Goblet in the Classroom\", \"Goblets II\", \"Harry Potter and the Goblet of Fire Movie Poster Book\", \"Charodeǐnaia chasha\", \"Commemorative goblets\", \"\\\" Goblets II\\\" ..\", \"The migration of symbols\", \"Greentown glass\", \"The fisherman and the goblet\", \"Froth and Goblets\", \"Faire semblant c'est mentir\", \"Dream-goblets, and other poems\", \"Harry Potter and the Goblet of Fire Book of Creatures\", \"The magic goblet\", \"Study Guide : Harry Potter and the Goblet of Fire\", \"Goblet\", \"Goblet\", \"Goblet\", \"American and Canadian Goblets\", \"The Squirrel's Goblet\", \"The migration of symbols\", \"A fairy in the goblet\", \"Harry Potter and the Goblet of Fire (SparkNotes Literature Guide)\", \"Demetrius and the golden goblet\", \"Murder and the Golden Goblet\", \"Medici Goblets\", \"Seventh Goblet\", \"Turning Goblets\", \"Goblets 2\", \"King's Goblet\", \"Platinum Goblet\", \"Isherus Goblet\", \"Seventh Goblet\", \"Goblet Notebook\", \"Goblets 2\", \"Cultural Goblet\", \"Lectures on the origin and growth of the conception of God as illustrated by anthropology and history\", \"The second Goblet price guide\", \"American price guide to Unitt's Canadian & American goblets\", \"Harry Potter and the Goblet of Fire, Themes From\", \"The golden goblet\", \"The Gottlehrling Goblet\", \"The goblet of Salobreña\", \"The Golden Goblet\", \"Migration des Symboles\", \"Political geography and the world map\", \"A comparative study of certain goblet cells\", \"Cat & Cat Adventures\", \"The golden goblet\", \"The Golden Goblet\", \"The Golden Goblet\", \"Goblet of Life\", \"Goblets of Cream\", \"Seven Enchanted Goblets\", \"Goblet of Infinity\", \"The Cyclops Goblet\", \"Beakers, tankards, goblets\", \"The golden goblet\", \"The stolen goblet\", \"The king's goblet\", \"The wonderful goblet\", \"A crystal goblet & the dragon\", \"Goblet of Life\", \"Goblet of Life\", \"Golden Goblet\", \"Wine in a crystal goblet\", \"Wine in a Venetian Goblet\", \"Harry Potter and the Goblet of Fire Coloring Book\", \"Harry Potter and the Goblet of Fire Sticker Book\", \"Greentown glass\", \"How Belgium saved Europe\", \"The blue goblet\", \"Geviʻe zahav\"]}"
        },
        {
            "role": "tool",
            "content": "3"
        }
    ],
    "tools": [
        {
            "type": "function",
            "function": {
                "name": "roll_dice",
                "description": "Roll dice.\nReturns:\n    int: The result of rolling a six-sided die (1-6).",
                "parameters": {
                    "type": "object",
                    "properties": {}
                }
            }
        },
        {
            "type": "function",
            "function": {
                "name": "search_books",
                "description": "Search for books on Open Library.\nArgs:\n    query: The search term (book title, author, etc.)\nReturns:\n    dict: Search results from Open Library API",
                "parameters": {
                    "type": "object",
                    "required": [
                        "query"
                    ],
                    "properties": {
                        "query": {
                            "type": "string"
                        }
                    }
                }
            }
        }
    ]
}
  • Response 2
{
    "model": "qwen2.5:7b",
    "created_at": "2026-06-07T16:49:18.580605Z",
    "message": {
        "role": "assistant",
        "content": "Based on your search, here are some book titles containing the word \"goblet\":\n\n1. The golden goblet\n2. Harry Potter and the Goblet of Fire\n3. Selections From Harry Potter and the Goblet of Fire. Instrumental Solos\n4. Benjamin and the Silver Goblet\n5. The Golden Goblet\n\nAnd so on.\n\nSince you rolled a 3, I will get the third book title suggestion from the list: \"Selections From Harry Potter and the Goblet of Fire. Instrumental Solos\"."
    },
    "done": true,
    "done_reason": "stop",
    "total_duration": 8283583041,
    "load_duration": 161865416,
    "prompt_eval_count": 1201,
    "prompt_eval_duration": 3576242000,
    "eval_count": 118,
    "eval_duration": 4523046000
}