Note
The Chinese and English guides cover the same model capabilities and main workflows. Sample inputs, prompts, output fields, and validation details may differ; follow the code in the guide you are reading. The companion Python scripts and Notebooks follow the Chinese guide.
1. Understand the model and tool calling
Step 5 Preview accepts messages and generates replies, and it can also request tool calls in its response. An application supplies descriptions of available functions in tools. The model returns a function name and arguments, the application executes the function and sends back the result, and the model continues from there. Model overview · Tool-calling protocol
This division of responsibilities is central to tool integration: the model chooses calls, while the program validates arguments, executes functions, and returns results. For calculations or data queries, actual values come from local functions or backend services. Call identifiers in the conversation connect each request to its result.
This guide is for developers who write Python functions and want to connect a model to existing services. You will implement a tool for addition and division, focusing on tool descriptions, tool_choice, call limits, and result messages.
Workflow: Initialize the client → Define the tool interface → Implement execution → Complete the calling loop → Replace the business function.
2. Prerequisites and setup
2.1 Prerequisites
Required knowledge: Familiarity with Python functions, dictionaries, and JSON.
- Python 3.10 or later.
- A StepFun API key with access to step-5-preview.
2.2 Create a Python environment
For an existing project, you can use its current virtual environment. For a new project, create an environment in any working directory. Run the following in a macOS or Linux terminal:
In Windows PowerShell, run:
After activation, confirm the Python version with the following command. All subsequent python commands refer to this environment’s interpreter.
2.3 Dependencies
This example uses only the Python standard library.
2.4 Configure credentials and run the example
Set STEP_API_KEY in the terminal that will run the example. Replace YOUR_STEP_API_KEY with your API key.
macOS / Linux:
Windows PowerShell:
Run the Python commands below in the same terminal. For a Notebook, start Jupyter from that terminal so its kernel inherits the variable. If the variable is unset, the code prompts for the key with input hidden.
Set REGION = "cn" for the China region, which uses .com endpoints, or "global" for the international region, which uses .ai endpoints. Use a key issued for the selected region.
Create tool_demo.py in your project and copy this guide's Python code blocks into that file in order. Run the bash and powershell blocks only in a terminal. After adding the definitions and calls, run:
2.5 Initialize the connection and credentials
BASE_URLS stores the regional endpoints, and REGION selects the endpoint for this run. get_key() prompts only when the environment variable is missing, which suits terminal use. A service can read the key directly from its deployment environment.
2.6 Set the HTTP timeout and read responses
The following post() function is the shared request helper for this guide. HTTP_TIMEOUT_SECONDS is a client setting that limits the wait for a network operation; configure it separately from reasoning effort or output length. The function returns the response bytes, the elapsed time through the end of the response read, and the content type.
chat() parses a chat response into a dictionary, response_record() extracts the final content and usage, and save_json() saves the results. In an existing project, these can form part of your response-handling layer. They do not change the capability parameters sent to the model.
3. Implement a complete tool call
3.1 Describe the available tool
TOOL is the interface description sent to the model. It includes the function name, purpose, and parameters. The example limits the interface to two operations and two numeric arguments so you can see how model-supplied arguments map to a Python function.
3.2 Implement the function your program executes
execute() dispatches by tool name, validates the arguments, and performs the operation. It returns a serializable object for the model to read in the next turn. The program executes only the explicitly defined operations, not arbitrary code generated by the model.
3.3 Connect the request, execution, and result
Each model request includes the conversation so far and the tool description. When tool_calls arrives, first preserve that assistant message, then execute each call and append a role="tool" message. tool_call_id associates the result with the original call. When there are no new tool calls, read the final answer.
Define the final-result handling rules first, then configure the calling strategy and loop limits. trace records responses and elapsed time; tool_calls records the arguments and results of actual executions.
Parameter group: tool selection and client budgets. tool_choice="auto" lets the model decide whether to call a tool based on the input. max_rounds and max_tool_calls are local loop limits for model-request rounds and actual tool executions, respectively. They are not sent as model parameters. Adjust them independently to meet your project's latency and cost requirements.
After the model selects a tool, the program appends the original assistant message, then the result with its matching tool_call_id. This order allows the next request to associate the result with the correct call. Network errors propagate to the calling entry point, where your application decides how to handle them.
3.4 Run a two-step calculation
Ask the model to add 18 and 6, then divide by 4. The final result should be 6. The tool execution records expose the intermediate and final values computed by the program.
4. Understand the calling strategy and loop state
Send a simple greeting to observe the auto strategy. When a task needs no calculation, the model can return a final answer directly.
The greeting result should have an empty tool_calls list and an answer starting with "Hello", ignoring case. The check verifies both the greeting and the absence of tool calls.
5. Inspect actual execution records
For the calculation task, check three things together: the final value is 6, the tool was actually executed, and the execution results support that value. The following code extracts those details and saves the full trace so you can track calls in your own project.
Each elapsed time in trace measures the full model response. Tool execution here is local arithmetic. For a remote query, you can measure tool latency separately inside execute(). Final-answer format, actual execution, and loop-round count answer different questions and should be inspected separately.
6. Replace the input and integrate your own tool
First change the input to (20 + 8) / 4, update the reference answer to 7, and run the existing loop.
To integrate your own tool, update the name and schema in TOOL, the dispatch and implementation in execute(), and the business rules used to check the final result. Retain result messages, call identifiers, and round limits. The model selects calls, your service executes them, and the application continues using actual tool results.
API reference: StepFun tool calling.
7. Companion code and starter project
This example includes a Jupyter Notebook, an editable Python script, and a starter project for running and adapting the code. Browse the example directory for all files, or see the setup and launch guide for instructions.
To use the launchers, obtain the complete example directory from the current repository and keep its relative paths intact. Initial setup requires an internet connection. The supplied Notebook, Python script, launch menu, and setup guide currently follow the Chinese version. To reproduce this guide's English prompts, sample inputs, and result fields, use the Python code blocks in this guide.
