AI on Kubernetes · GPU/NPULesson 18 of 20
Serving an LLM Agent That Calls Tools
An agent is an application that lets the model ask for tools and keeps calling the model until it has an answer. The model never runs a tool itself. It writes a tool call as JSON, and your app runs it, so the app is where limits and safety live.
Model calls0
Input tokens—
Loop statenew task
RequestResponse / reportWatchAppears in a later step
What you will learn
- The loop: messages + tools, tool call, run, append, call again
- How MCP servers expose tools to the app
- Why every turn adds latency and tokens
- Step limits, timeouts, least privilege and sandboxing
- Prompt injection arriving inside tool results
- Human approval and per-step tracing
Agent risks and controls
| Risk | What it looks like | Control |
|---|---|---|
| Runaway loop | Same tool call repeats, task never ends | Max steps per task, per-tool timeout, task deadline |
| Cost and latency growth | Each turn resends the growing history | Track tokens per task, keep tool results short, use prefix caching |
| Over-privileged tools | Tool uses a shared admin credential | User-scoped, read-only where possible, separate credentials per tool |
| Code or browsing tools | Tool can reach internal services | Sandbox with no secrets and restricted network egress |
| Prompt injection | Tool result contains instructions | Treat output as data, limit tool power, approval for risky actions |
| Unclear failures | Wrong answer, no idea which step | One trace per task, one span per model and tool call |
Try it yourself
# Serve a model with tool-call parsing (the parser must match the model family)
vllm serve Qwen/Qwen2.5-7B-Instruct --enable-auto-tool-choice --tool-call-parser hermes
# One loop turn: the model returns a tool call, it does not run it
curl -s localhost:8000/v1/chat/completions -H 'Content-Type: application/json' -d '{
"model": "Qwen/Qwen2.5-7B-Instruct",
"messages": [{"role": "user", "content": "Where is order 1042?"}],
"tools": [{"type": "function", "function": {"name": "get_order",
"description": "Look up an order by id",
"parameters": {"type": "object", "properties": {"order_id": {"type": "string"}}, "required": ["order_id"]}}}]
}' | jq '.choices[0] | {finish_reason, tool_calls: .message.tool_calls}'
# List and call tools on an MCP server with the MCP Inspector CLI
npx @modelcontextprotocol/inspector --cli node path/to/server/index.js --method tools/list
npx @modelcontextprotocol/inspector --cli node path/to/server/index.js \
--method tools/call --tool-name get_order --tool-arg order_id=1042
Check yourself
Further reading: MCP specification · MCP tools · vLLM tool calling · From API Request to Response · How a RAG Pipeline Answers From Your Documents