run_python tool, and attach that tool to an agent. When the agent
decides it needs live data, SeekrFlow runs your function and feeds the result
back into the conversation.
The example function calls a public weather API (wttr.in) and
returns the forecast for a single ZIP code, so you can see the full round trip
from agent to your code to a third-party service and back.
What you’ll build
An agent that:- Exposes a custom Python function as a
run_pythontool. - Calls the tool whenever the user asks about the weather.
- Runs your function, which requests live data from an external API.
- Reports back exactly what the API returned.
Prerequisites
- A SeekrFlow API key, set as the
SEEKR_API_KEYenvironment variable - Python 3.8 or later
- The SeekrFlow SDK:
pip install seekrai
Build it
1
Write the weather function
Save the following as
weather_fn.py. This is an ordinary Python function with
no SeekrFlow dependencies. The docstring matters: SeekrFlow uses it to tell the
agent what the tool does and when to call it, so write it the way you would
write a tool description.The function catches its own errors and returns them as a string instead of
raising. That way a failed API call comes back to the agent as readable text it
can relay, rather than crashing the run.
2
Set up the client
Create a second file,
weather_agent.py, for the agent itself. Start with the
imports, configuration, and client. Keep weather_fn.py in the same directory,
since you will upload it by path in a later step.This recipe uses
meta-llama/Llama-3.1-70B-Instruct. A larger instruct model
follows the “always call the tool” instruction reliably and handles the
tool-calling handshake well. Any instruct model that supports tool calling
works here, so you can substitute a smaller or newer model such as
meta-llama/Llama-3.3-70B-Instruct to trade some quality for speed and cost.3
Add two helper functions
These helpers wait for asynchronous operations to finish: one polls a run until
it completes, and one polls the agent until it is
Active after promotion. Add
them to weather_agent.py.4
Register the custom function
Upload
weather_fn.py so SeekrFlow can run it. The returned function ID
connects your code to the tool in the next step.5
Create the run_python tool
Wrap the registered function in a
run_python tool. The description is what
the agent sees when it decides whether to call the tool, so make it specific.6
Create the agent
Create the agent, attach the tool by ID, and give it instructions that tell it
to always call the tool for weather questions.
7
Promote the agent
Promote the agent to deploy it, then wait for it to become
Active.8
Send a prompt
Send a prompt and print the agent’s reply. Because the instructions require it,
the agent calls your function, which requests live data from wttr.in, and then
reports the result.
9
Run the script
Run the finished script:You should see the agent report the current Arlington, VA forecast, sourced live
from the weather API through your function.
Clean up resources (optional)
Promoted agents and their tools stay in your account until you remove them. When you are done experimenting, tear down the resources this recipe created. This deletes every agent, tool, and function that matches the names set at the top ofweather_agent.py, so run it only against a scratch account.
Next steps
- Accept parameters. The function takes no arguments, so it always reports
the same ZIP code. Add a
zip_code: strparameter and describe it in the docstring so the agent can supply the location from the user’s question. - Call your own API. Swap the wttr.in request for a call to an internal service to give the agent access to your own live data.
- Add more tools. Attach several functions to one agent and let it choose which to call based on the question.