Streaming and tools
A model talks; a tool does things. This lesson shows the loop that joins the two.
Two features turn a single call into a product: streaming, so the user doesn't wait, and tools, so the model can do things.
Streaming
With stream=true the answer arrives in pieces and the first words appear sooner. For any live chat interface, it's the right choice.
resp = client.chat.completions.create(
model="deepseek/deepseek-v4-flash",
messages=[{"role": "user", "content": "Write a short story"}],
stream=True,
)
for chunk in resp:
if chunk.choices:
print(chunk.choices[0].delta.content or "", end="", flush=True)The last piece before [DONE] has usage and its choices is empty. That's why the loop checks choices first.
Tools
A model can't connect to your database by itself, but it can say which function to call with what input. That's function calling.
- You send the request with the tool definitions.
- If the model wants a tool,
finish_reasonistool_calls. - You run the function yourself.
- You add the result to the messages with role
tooland the sametool_call_id, and send again.
Tool schemas are part of the prompt and count as input tokens on every call of that conversation.
Tools we run for you
Three tools with the iranrouter: prefix you don't run yourself. We search or read, give the result to the model, and one final answer comes to you.
| Name | What it does |
|---|---|
iranrouter:web_search | Searches the public web |
iranrouter:web_fetch | Reads the text of one public page |
iranrouter:datetime | Date and time in Tehran and UTC, with the Jalali date |
resp = client.chat.completions.create(
model="deepseek/deepseek-v4-flash",
messages=[{"role": "user", "content": "What is today's date?"}],
tools=[{"type": "function", "function": {"name": "iranrouter:datetime"}}],
)
print(resp.choices[0].message.content)The model must support function calling. Each round of this loop is a whole request, so one answer may show several rows in your usage. The loop is capped so it can't run away.
web_fetch opens only an address that appears exactly in your messages or an earlier search result. That stops a hostile page from walking your conversation out through a URL.Call the datetime tool0 of 3 steps
- Run the sample above with your key.
- See that the answer has the right date.
- Count this request's rows in the logs.
What you learned
- Streaming improves the experience, and usage comes in the last piece.
- You run ordinary tools; we run the
iranrouter:ones. - Next: making your app resilient when models go down.
Keep your place
Sign in to the panel and each lesson and your path are saved, so you continue from the same spot on any device.