AIOps Peach

Agents · Module 02

Tool Calling & Tavily Search

How a model asks your code to run a function: tool schemas, the tool-call loop, LangChain tools, web search with Tavily, parallel calls and error handling.

Học tương tác →

Học xong bạn làm được

Bài học

  1. What is tool calling?

    Tool calling (function calling) lets an LLM decide when a tool is needed, call it with JSON-schema parameters, read the result and keep reasoning. It extends the model past its knowledge cutoff, gives it real-time data and lets it act on other systems. With the OpenAI SDK you define tools, send them with the request, run the returned tool call yourself and send the result back.

  2. LangChain tools

    LangChain wraps a function as a tool either with the Tool class (name, func, description) or with the @tool decorator, which takes the name from the function and the description from its docstring. LangChain Community ships ready-made tools such as DuckDuckGo search, Wikipedia, a Python REPL and file read/write. bind_tools gives a chat model the tools' schemas.

  3. Tavily search

    Tavily is a search engine built for LLMs and RAG - clean, relevant, real-time results pre-formatted for a model, with web and news search, an answer mode and context optimisation. You install tavily-python, search with TavilyClient and tune depth, result count, domains, answer, raw content and images. The langchain-tavily package wraps it as the TavilySearch tool for agents.

  4. Tool chaining, error handling and optimization

    In tool chaining one tool's output becomes another's input. API failures are retried with tenacity and exponential backoff, and hanging tools are cut off with asyncio.wait_for. Tool calls are optimised by caching results (lru_cache, or a dictionary with a one-hour TTL), rate limiting with the ratelimit package, and batching searches concurrently with asyncio.gather.

  5. Custom tools, security and tool descriptions

    A custom tool subclasses BaseTool with a name, a description, a Pydantic args_schema and _run / _arun methods. API keys are never hardcoded - they come from environment variables (a .env file via load_dotenv) or a secret manager such as Azure Key Vault. A clear, detailed tool description says what the tool does, when to use it, what input it expects and what it returns.

  6. Structured outputs and modern tool patterns

    Constrained decoding makes a model's output follow a JSON schema - OpenAI strict json_schema, Claude tool_use or JSON mode, XGrammar in vLLM - and Pydantic models parse straight into objects. Use structured outputs for pure data extraction and tool calling for multi-action agents. MCP is the open standard that connects LLM hosts to tool servers. ToolNode runs independent tool calls in parallel.

  7. Assignment: Tool Calling & Tavily Search Integration

    Add a Tavily web-search tool to the multi-expert research agent, with parallel calls, retries, caching and rate limiting.