AI Agents

Tool use, agent loops, and when agents beat simple prompts

Overview

An AI agent is an LLM-driven loop that plans, calls tools (functions/APIs), observes results, and repeats until a goal is met. Frameworks like Vercel AI SDK, LangGraph, or custom loops handle tool schemas and termination.

Syntax / Usage

Basic agent loop:

1. User gives goal
2. LLM decides: respond OR call tool(s)
3. Execute tools → append results to conversation
4. LLM observes → repeat until done or max steps
5. Return final answer

Tool definition (OpenAI function-calling style):

{
  "name": "get_order_status",
  "description": "Look up order by ID",
  "parameters": {
    "type": "object",
    "properties": {
      "order_id": { "type": "string" }
    },
    "required": ["order_id"]
  }
}

When to use agents vs simple prompts:

Simple promptAgent
Summarize, classify, rewriteMulti-step research across systems
Single API lookup you controlDynamic tool selection
Low latency, predictable costTasks needing iteration/refinement

Guardrails: max iterations (5–10), allowlist tools, validate arguments, require human approval for destructive actions.

Examples

Minimal tool loop (pseudocode):

for (let step = 0; step < MAX_STEPS; step++) {
  const response = await llm.chat({ messages, tools });
  if (!response.tool_calls) return response.content;
  for (const call of response.tool_calls) {
    const result = await runTool(call.name, call.arguments);
    messages.push({ role: "tool", content: JSON.stringify(result) });
  }
}

Use agents for internal ops copilots; prefer RAG + structured outputs for customer-facing Q&A.

Common Mistakes

  • Agent for every feature—adds latency, cost, and failure modes
  • Unbounded loops without step limits or timeouts
  • Tools with side effects (delete, charge) without confirmation
  • Passing raw tool output back without size limits—context overflow
  • No logging of tool calls for debugging and audit trails

See Also

prompt-engineering ai-apis rag-basics responsible-ai