Skip to main content
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
Tools · least privilegeUserasks in chatAgent appruns the loopLLM serverreturns tool callsMCP servertools/list · tools/callOrders DBread-only roleWeb pageuntrusted contentHuman approvalrisky actionsTracingone span per steporder 1042?
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​

RiskWhat it looks likeControl
Runaway loopSame tool call repeats, task never endsMax steps per task, per-tool timeout, task deadline
Cost and latency growthEach turn resends the growing historyTrack tokens per task, keep tool results short, use prefix caching
Over-privileged toolsTool uses a shared admin credentialUser-scoped, read-only where possible, separate credentials per tool
Code or browsing toolsTool can reach internal servicesSandbox with no secrets and restricted network egress
Prompt injectionTool result contains instructionsTreat output as data, limit tool power, approval for risky actions
Unclear failuresWrong answer, no idea which stepOne 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

  1. The model returns a tool call for refund. Who actually executes the refund?
  2. A task takes five model calls. Why does it cost much more than five times a single short request?
  3. A fetched web page tells the model to refund all orders. Which defense works even if the model is fooled?

Further reading: MCP specification · MCP tools · vLLM tool calling · From API Request to Response · How a RAG Pipeline Answers From Your Documents