Agents · October 21, 2026 · 4 min read
Building your first AI agent with tool calling
Learn how to build and deploy an AI agent using Python tool calling and Podstack's serverless GPU infrastructure for high-performance inference.
Building your first AI agent can feel like a daunting leap from basic prompt engineering. While a standard LLM can generate text, an agent can interact with the world by executing code, searching the web, or querying databases. The bridge between "talking" and "doing" is a mechanism called tool calling (or function calling).
In this guide, we will walk through building a functional AI agent that uses Python tools to solve math problems and fetch real-time data, and then deploy it as a high-performance endpoint.
Understanding Tool Calling
At its core, tool calling is a structured dialogue. Instead of the LLM just responding with text, the model is trained to recognize when a user’s request requires an external tool. It then outputs a structured JSON object containing the function name and the arguments to be used.
Your application executes the function locally, feeds the result back to the LLM, and the LLM summarizes the final answer. This loop—Reasoning -> Action -> Observation—is what makes an agent "intelligent."
Step 1: Defining Your Tools
To start, we need to define the Python functions our agent can use. For this example, let's create a tool that calculates the square root of a number (to demonstrate precision) and a tool that simulates fetching live stock prices.
import math
def get_stock_price(ticker: str):
# In a real scenario, you'd call an API here
prices = {"POD": 150.25, "GPU": 88.40}
return prices.get(ticker.upper(), "Ticker not found.")
def calculate_sqrt(n: float):
return math.sqrt(n)
# Define the tools for the LLM
tools = [
{
"type": "function",
"function": {
"name": "get_stock_price",
"description": "Get the current stock price for a given ticker",
"parameters": {
"type": "object",
"properties": {
"ticker": {"type": "string"}
},
"required": ["ticker"]
}
}
},
{
"type": "function",
"function": {
"name": "calculate_sqrt",
"description": "Calculate the square root of a number",
"parameters": {
"type": "object",
"properties": {
"n": {"type": "number"}
},
"required": ["n"]
}
}
}
]
Step 2: The Agent Logic
The agent needs to process the user's input, decide if a tool is needed, and handle the execution. When using Podstack’s serverless inference endpoints, you can pass these tool definitions directly to models like Llama 3 or Mixtral.
Here is how you handle the tool calling loop:
import os
import requests
# Your Podstack API Key
api_key = os.getenv("PODSTACK_API_KEY")
def run_agent(prompt):
messages = [{"role": "user", "content": prompt}]
# 1. Ask the model for a response
response = requests.post(
"https://podstack.ai/api/v1/chat/completions",
headers={"Authorization": f"Bearer {api_key}"},
json={
"model": "podstack/kimi-k3",
"messages": messages,
"tools": tools
}
).json()
message = response['choices'][0]['message']
# 2. Check if the model wants to call a tool
if message.get("tool_calls"):
for tool_call in message["tool_calls"]:
func_name = tool_call["function"]["name"]
args = tool_call["function"]["arguments"]
# Execute the local function
if func_name == "get_stock_price":
result = get_stock_price(**args)
elif func_name == "calculate_sqrt":
result = calculate_sqrt(**args)
# 3. Send the tool result back to the model
messages.append(message)
messages.append({
"role": "tool",
"tool_call_id": tool_call["id"],
"content": str(result)
})
# 4. Get the final synthesized response
final_response = requests.post(
"https://podstack.ai/api/v1/chat/completions",
headers={"Authorization": f"Bearer {api_key}"},
json={"model": "podstack/kimi-k3", "messages": messages}
).json()
return final_response['choices'][0]['message']['content']
return message['content']
print(run_agent("What is the square root of 144 and the price of POD stock?"))
Step 3: Deploying as a GPU Function
Running agents locally is fine for testing, but for production, you want low-latency execution and high availability. This is where Podstack shines. Instead of managing complex infrastructure, you can deploy your agent logic as a Python GPU function.
Using the Podstack API, you can wrap your agent code and deploy it to an on-demand GPU instance. This allows your agent to handle heavy inference loads or run additional ML models (like embedding generation) alongside the tool calling logic.
Podstack does not ship a command-line interface. Use the console, the Python package `podstack-sdk`, or the API at https://podstack.ai/api/v1 with a `ps_live_` key.
Podstack handles the scaling automatically. You only pay for the seconds the GPU is active while processing your agent's requests.
Practical Tips for Tool Calling
- Strict Schemas: Ensure your tool descriptions are verbose. The LLM relies solely on the
descriptionfield to understand when to use a tool. - Error Handling: If a tool fails (e.g., an API timeout), pass the error message back to the LLM. Often, the model can "self-correct" and try a different set of arguments.
- State Management: For multi-turn conversations, ensure you maintain the message history correctly, including the
toolrole messages. - Security: Never give a tool direct access to
eval()or sensitive file system commands without strict sanitization.
Conclusion
Building an AI agent is no longer a research project; it's an engineering task. By leveraging tool calling, you transform an LLM from a passive knowledge base into an active participant in your workflow.
With Podstack, the transition from a local Python script to a production-grade agent is seamless. By using our per-second billing and serverless GPU functions, you can scale your agent's reasoning capabilities without the overhead of managing clusters. Ready to deploy? Grab your PODSTACK_API_KEY and start building the next generation of autonomous tools today.