====== Tool Integration Patterns ======
**Tool integration patterns** define the standard approaches for connecting AI agents to external tools, services, and APIs(([[https://arxiv.org/abs/2210.03629|Yao, S. et al. "ReAct: Synergizing Reasoning and Acting in Language Models." arXiv:2210.03629, 2022.]])). These patterns address how tools are described to agents, how agents discover and select them, how invocations are structured, how results feed back into reasoning, and how errors are handled. Well-designed tool integration is essential for building agents that operate effectively in real-world environments.
graph LR
UQ[User Query] --> LLM1[LLM Reasoning]
LLM1 --> TS[Tool Selection]
TS --> TE[Tool Execution]
TE --> Result[Result]
Result --> LLM2[LLM Synthesis]
LLM2 --> Resp[Response to User]
LLM2 -.->|Needs more data| TS
style LLM1 fill:#e1f5fe
style LLM2 fill:#e1f5fe
style TE fill:#fff3e0
===== Function Calling Pattern =====
The most widely adopted pattern, where LLM providers expose a native mechanism for structured tool invocation:
* The developer defines tools as JSON Schema descriptions (name, parameters, descriptions)
* The model receives these schemas alongside the conversation context
* When the model determines a tool is needed, it generates a structured JSON call (not free-form text)
* The application executes the call and returns results to the model
* The model incorporates results into its response
This pattern is implemented by [[function_calling|OpenAI]] ([[function_calling|function calling]] / tool use), Anthropic (tool use), Google ([[function_calling|function calling]]), and most major providers(([[https://arxiv.org/abs/2302.04761|Schick, T. et al. "Toolformer: Language Models Can Teach Themselves to Use Tools." arXiv:2302.04761, 2023.]])). It replaced earlier fragile approaches based on [[prompt_engineering|prompt engineering]] and regex parsing.
The following example shows how to define a tool registry, let the model select tools, and execute them:
Tool definition, selection, and execution pattern
from [[openai|openai]] import [[openai|OpenAI]]
import json
client = [[openai|OpenAI]]()
Tool registry: map names to callable functions
def search_database(query: str, limit: int = 5) -> str:
return json.dumps({"results": [f"Result for '{query}' #{i}" for i in range(limit)]})
TOOL_REGISTRY = {"search_database": search_database}
TOOLS = [{
"type": "function",
"function": {
"name": "search_database",
"description": "Search the product database",
"parameters": {
"type": "object",
"properties": {
"query": {"type": "string", "description": "Search query"},
"limit": {"type": "integer", "description": "Max results"},
},
"required": ["query"],
},
},
}]
Send request and execute any tool calls the model returns
messages = [{"role": "user", "content": "Find laptops under $500"}]
response = client.chat.completions.create(model="gpt-4o", messages=messages, tools=TOOLS)
for tool_call in response.choices[0].message.tool_calls or []:
fn = TOOL_REGISTRY[tool_call.function.name]
result = fn(**json.loads(tool_call.function.arguments))
messages.append(response.choices[0].message)
messages.append({"role": "tool", "tool_call_id": tool_call.id, "content": result})
Get final response incorporating tool results
final = client.chat.completions.create(model="gpt-4o", messages=messages, tools=TOOLS)
print(final.choices[0].message.content)
===== Model Context Protocol (MCP) =====
[[anthropic_context_protocol|MCP]] extends the [[function_calling|function calling]] pattern into a full client-server protocol:
* **Server registration:** Tools, resources, and prompts are registered on [[mcp_servers|MCP servers]]
* **Dynamic discovery:** Clients discover available tools at runtime rather than hardcoding schemas
* **Transport-agnostic:** Works over stdio (local) or Streamable HTTP (remote)
* **Standardized interface:** Any MCP client can connect to any MCP server
MCP is particularly powerful for enterprise environments where tools span multiple services and need centralized management(([[https://[[github|github]])).com/modelcontextprotocol|Anthropic. "Model Context Protocol." [[github|github]].com/modelcontextprotocol, 2024.]])). By 2025, it became the dominant standard for AI-tool connectivity. In practice, tool-using agents integrate with diverse enterprise systems such as Slack, GitHub, Linear, QuickBooks, Stripe, and Meta Ads to execute real workflows(([[https://tldr.tech/ai/2026-04-13|TLDR AI. "Tool-Using Agents." tldr.tech, April 2026.]])).
===== Plugin Architectures =====
Plugin systems treat tools as [[modular|modular]], dynamically-loadable components:
* **[[openai|OpenAI]] Plugins** (2023): Early approach allowing ChatGPT to access third-party services via API manifests
* **[[langchain|LangChain]] Tools:** Extensible tool classes with standardized input/output interfaces
* **[[semantic_kernel|Semantic Kernel]] Plugins:** Microsoft's approach using skill-based plugin registration(([[https://arxiv.org/abs/2205.00445|Karpas, E. et al. "MRKL Systems: A [[modular|modular]])), neuro-symbolic architecture." arXiv:2205.00445, 2022.]]))
* **Custom plugin loaders:** Framework-specific systems that scan directories or registries for tool definitions
Plugin architectures enable extensibility without modifying the core agent, supporting marketplace-style tool distribution.
===== ReAct Pattern =====
The **ReAct (Reasoning + Acting)** pattern interleaves reasoning and tool use in an iterative loop:
- **Thought:** The agent reasons about what action to take next
- **Action:** The agent selects and invokes a tool
- **Observation:** The agent observes the tool's output
- **Repeat:** Continue until the task is complete or a final answer is reached
ReAct grounds agent responses in real-world data and is the foundation for most modern agent frameworks. It naturally supports multi-step tool use, error recovery, and adaptive planning. See [[react_framework|ReAct Prompting]] for the original paper by [[https://arxiv.org/abs/2210.03629|Yao et al., 2022]].
===== Tool Selection Strategies =====
How agents choose which tool to use:
* **Schema matching:** The LLM selects tools based on JSON Schema descriptions provided in context
* **Hierarchical routing:** Supervisor agents delegate to specialist agents based on domain (25-40% improvement in task completion)
* **[[semantic_search|Semantic search]]:** Tool descriptions are embedded and retrieved via similarity matching when the tool set is too large for context
* **Tool search:** Deferred tool loading where rarely-used tools are loaded on demand (supported by [[gpt_54|GPT-5.4]]+)
* **Cost-based selection:** Routing to smaller/cheaper models for simple tool calls, reserving larger models for complex reasoning
===== Error Handling Patterns =====
Robust tool integration requires systematic error handling:
* **Retry with backoff:** Automatic retries on transient failures (rate limits, timeouts)
* **Fallback chains:** If the primary tool fails, fall back to an alternative (e.g., different search API)
* **Graceful degradation:** Continue reasoning with partial results rather than failing completely
* **Validation loops:** Verify tool outputs against expected schemas before feeding back to the model
* **Sandboxing:** Execute tools in isolated environments to prevent cascading failures
* **Human escalation:** Route to human operators when tool errors exceed retry thresholds
===== Modern Trends (2025) =====
* **Multi-agent tool sharing:** Agents in a [[multi_agent_systems|multi-agent system]] share tool access through centralized registries
* **Parallel tool execution:** 30-60% faster execution by invoking independent tools simultaneously
* **Auto-hardening:** Frequently-used tool patterns automatically compiled into deterministic code paths
* **Observability:** End-to-end tracing of tool calls through agent reasoning chains
===== Related Pages =====
* [[anthropic_context_protocol|Model Context Protocol (MCP)]]
* [[function_calling|OpenAI Function Calling]]
* [[react_framework|ReAct Prompting]]
* [[tool_utilization|Tool Utilization]]
* [[mrkl_systems|MRKL Systems]]
* [[toolformer|Toolformer]]
===== See Also =====
* [[simple_cli_vs_elaborate_tool_infrastructure|Simple CLI vs Elaborate Tool Infrastructure]]
* [[agent_design_patterns|Agent Design Patterns]]
* [[common_tool_definitions|Common Tool Definitions]]
* [[tool_abstraction_interface|Unified Tool Abstraction Interface]]
* [[tool_use_orchestration|Tool Use and Orchestration]]
===== References =====