Once you've defined tools and made them technically available to your agent, the next pivotal step is to enable the agent's Large Language Model (LLM) brain to understand when and how to use them. LLMs, by themselves, are masters of language but are not inherently aware of external functionalities like a calculator or a web search API you've just connected. This is where careful prompt engineering comes into play. Your prompt is the primary instruction manual you provide to the LLM, guiding its reasoning and its interaction with these new capabilities.
Think of the LLM as a very smart assistant who can follow instructions precisely. If you want this assistant to use a new tool, you first need to tell them that the tool exists, what it does, and how to request its use. This information is typically embedded within the main prompt you send to the LLM.
The first part of instructing your agent about tools is to simply list them in the prompt. This list acts as a menu of available actions beyond standard text generation. For each tool, you need to provide enough information for the LLM to make an informed decision about using it.
A common practice is to include a dedicated section in your prompt that outlines the tools. For example:
You have access to the following tools:
1. **Calculator**: Useful for performing mathematical calculations.
2. **SearchEngine**: Useful for finding real-time information or facts about topics you don't have in your knowledge base.
A name alone isn't enough. The LLM needs a clear and concise description of what each tool does, what kind of input it expects, and sometimes, what kind of output it will produce. The better the description, the more accurately the LLM will choose and use the tool.
Consider our "Calculator" tool. A good description might be:
Calculator
And for a "SearchEngine" tool:
SearchEngine
The key is to be specific enough for the LLM to understand the tool's utility without overwhelming it with excessive detail. You're trying to give the LLM enough context to match a user's request (or a sub-task it identifies) to the appropriate tool.
Once the LLM decides a tool is necessary, it needs a way to communicate this decision back to the agent's underlying code, which will then execute the tool. This requires a predefined format for signaling tool invocation. If the LLM just says "I think I need the calculator," your program won't know what to do next or what calculation to perform.
A robust and widely adopted method is to instruct the LLM to output its intention to use a tool in a structured format, most commonly JSON. This is because JSON is easy for programs to parse and understand.
You would add instructions to your prompt like this:
When you decide to use a tool, you must respond *only* with a JSON object in the following format:
{
"tool_name": "name_of_the_tool_to_use",
"tool_input": "the_input_string_for_the_tool"
}
If you can answer directly without using a tool, provide your answer as plain text.
Do not include any other text or explanation before or after the JSON object if you are calling a tool.
This instruction is critical. It tells the LLM:
tool_name
(to specify which tool) and tool_input
(to provide the necessary input for that tool).Without such a structured format, you'd be left trying to guess the LLM's intentions from its natural language response, which is far less reliable for automated systems.
Let's combine these elements into a more complete prompt for an agent that has a simple calculator tool.
Imagine the overall goal for the agent is to be a helpful assistant.
You are a helpful AI assistant. You can answer questions and perform tasks.
If you can answer directly, please do so.
You have access to the following tool:
- **Tool Name**: Calculator
- **Description**: Useful for performing mathematical calculations. Input should be a mathematical expression (e.g., '2+2', '10*5').
If you need to use the Calculator tool, you must respond *only* with a JSON object in the following format:
{
"tool_name": "Calculator",
"tool_input": "mathematical expression"
}
Do not add any extra commentary or text if you are using the tool.
If you are not using a tool, provide your answer directly as text.
User's request will follow.
Now, if the user asks, "What is 125 multiplied by 34?", the LLM, guided by this prompt, should ideally respond with:
{
"tool_name": "Calculator",
"tool_input": "125 * 34"
}
Your agent's code would then parse this JSON, execute the Calculator
tool with the input "125 * 34"
, get the result (e.g., 4250
), and then feed this result back to the LLM in a subsequent step to formulate the final answer for the user (e.g., "125 multiplied by 34 is 4250.").
When the LLM receives a user's query, along with your carefully crafted prompt containing tool information, it goes through a reasoning process. While the exact internal workings are complex, you can think of it as follows:
The following diagram illustrates this decision-making flow influenced by your prompt:
This diagram shows how the user's query, combined with the tool descriptions and usage instructions you provide in the prompt, allows the LLM to decide whether to answer directly or to format a request for your agent's system to use a tool. The observation from the tool is then typically fed back to the LLM.
Crafting prompts that reliably guide an LLM to use tools effectively often involves some iteration:
tool_input
format within the tool's description can be very helpful for the LLM. For instance, for a date formatting tool: "Input should be a date string like 'tomorrow' or 'next Friday'."By thoughtfully designing your prompts, you provide the LLM with the knowledge and the structured communication pathway it needs to leverage external tools. This significantly expands what your agent can accomplish, moving it from a pure text generator to a more capable system that can interact with and act upon the world around it, or at least, the digital systems you connect it to. The next step is to understand the logic the agent uses to make these tool selections more robustly.
Was this section helpful?
© 2025 ApX Machine Learning