Intermediate10 min
Tool-calling agents on Kimi K3
A two-tool agent loop with spend guardrails — the pattern behind every support bot and coding assistant.
1. Declare tools
Use the OpenAI tools schema. Kimi K3 and GLM-5.3 both speak it natively.
tools=[{"type": "function", "function": {
"name": "lookup_order",
"parameters": {"type": "object",
"properties": {"id": {"type": "string"}}}}}]2. Loop: call → execute → feed back
Cap at 8 iterations. Stream deltas so the UI feels alive while tools run.
for _ in range(8):
msg = step(messages) # may contain tool_calls
if not msg.tool_calls: break
messages += [msg, run_tools(msg)]3. Guard the furnace
Agents burn tokens. Per-key scopes, a hard spend limit (402s), and low-credit webhooks in Settings.
# Settings → spend limit → over-limit calls get 402 # Webhooks → credit.low → Slack