You've learned about the fundamental role tools play in extending an LLM agent's abilities and the importance of clear specifications. Now, it's time to put that knowledge into practice. This section will guide you through the process of crafting your first tool definition. A well-defined tool is the cornerstone of effective LLM interaction, ensuring the agent understands what the tool does, what information it needs, and what to expect in return.
We will focus on the definition aspect here. The actual Python code that implements the tool's logic will be covered in subsequent chapters. For now, our goal is to create a clear contract that an LLM can use.
Before we start, let's briefly review the main components of a tool definition. These components work together to provide a comprehensive understanding of the tool for the LLM agent.
Components typically found in a tool definition for an LLM agent.
Let's design a tool that can fetch the current weather for a given location. This is a common and practical example. An LLM agent could use such a tool to answer user queries like "What's the weather like in London?" or "Will I need an umbrella in Paris tomorrow?" (though our first version will stick to current weather).
Our tool will need:
The name should be descriptive yet concise. It's often used programmatically, so snake_case or camelCase is common. Let's call our tool: get_current_weather
.
The description is primarily for the LLM. It should clearly explain:
A good description for our get_current_weather
tool might be:
"Fetches the current weather conditions, including temperature, a brief description, and humidity, for a specified city. Use this tool when a user asks about the weather in a particular location. It can optionally take a state or country code to help disambiguate cities with the same name."
Notice how this description guides the LLM on its utility ("when a user asks about the weather") and hints at its parameters.
Tools need inputs to operate. We need to define each parameter the get_current_weather
tool expects. For each parameter, we should specify:
city
).string
, number
, boolean
).Many LLM frameworks use a JSON Schema-like structure for defining parameters. Let's define the inputs for our weather tool:
city
:
string
state_or_country_code
:
string
This tells the LLM that it absolutely needs a city
, but the state_or_country_code
is optional and should be used to provide more specific location information if available or needed.
Just as important as defining inputs is defining the structure of the data the tool will return. This helps the LLM (and you, the developer) understand what to expect back from the tool after it executes.
For our get_current_weather
tool, a successful execution might return:
location_found
:
string
temperature
:
number
(could be integer or float)unit
:
string
condition
:
string
humidity_percent
:
integer
error
:
string
(or null
)Defining the output, especially including potential error fields, makes the tool more predictable and easier for the LLM agent to handle various outcomes.
Now, let's put all these pieces together into a single, structured definition. The exact format can vary depending on the LLM framework you are using (like LangChain, LlamaIndex, or directly with models like OpenAI's GPT). A common approach is a JSON object.
Here’s how our get_current_weather
tool definition might look:
{
"name": "get_current_weather",
"description": "Fetches the current weather conditions, including temperature, a brief description, and humidity, for a specified city. Use this tool when a user asks about the weather in a particular location. It can optionally take a state or country code to help disambiguate cities with the same name.",
"input_schema": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "The name of the city for which to retrieve the weather, e.g., 'San Francisco', 'Tokyo'."
},
"state_or_country_code": {
"type": "string",
"description": "Optional. The state abbreviation (e.g., 'CA' for California) or the two-letter ISO country code (e.g., 'GB' for Great Britain) to disambiguate the city name if necessary."
}
},
"required": ["city"]
},
"output_schema": {
"type": "object",
"properties": {
"location_found": {
"type": "string",
"description": "The fully resolved location for which weather was found, e.g., 'London, GB'."
},
"temperature": {
"type": "number",
"description": "The current temperature."
},
"unit": {
"type": "string",
"description": "The unit of temperature provided (e.g., 'Celsius', 'Fahrenheit')."
},
"condition": {
"type": "string",
"description": "A brief textual description of the weather conditions (e.g., 'Sunny', 'Cloudy with showers')."
},
"humidity_percent": {
"type": "integer",
"description": "The current humidity as a percentage (e.g., 65 for 65%)."
},
"error": {
"type": "string",
"description": "An error message if the weather data could not be retrieved (e.g., 'City not found'). This field will be absent or null if the request was successful."
}
},
"required": ["location_found", "temperature", "unit", "condition", "humidity_percent"]
}
}
Note: Some frameworks might use "parameters" instead of "input_schema". The key is the structured definition of inputs and outputs. The
required
list in theoutput_schema
indicates fields expected upon success; theerror
field would typically be present only if something went wrong.
This structured definition serves several important purposes:
name
and description
help the LLM identify the right tool for a given user query or task.input_schema
, particularly the properties
and their individual description
fields, allows the LLM to understand what arguments are needed, their types, and how to format them. For example, if a user says "weather in Paris", the LLM knows to extract "Paris" for the city
parameter.output_schema
for the LLM's direct use in the same way as an input_schema
, defining it is a best practice. It clarifies the contract for the developer implementing the tool and helps in designing how the LLM should process the tool's results. It's also invaluable for testing and validation.As you craft your own tool definitions, remember these points derived from our example:
string
, number
, boolean
, object
, array
) helps in validation and ensures the tool receives data in the expected format.This hands-on exercise in defining a tool lays the groundwork for the next step: implementing the actual logic behind it. With a solid definition, you provide the LLM agent with the necessary information to use your custom capabilities effectively. In the following chapters, we'll explore how to bring these definitions to life with Python code.
Was this section helpful?
© 2025 ApX Machine Learning