Agentic Tool Use Patterns: Tool Calling in Production LLM Pipelines
A tactical breakdown of tool definition schemas, execution sandboxing, and output parsing patterns for production AI agents.
Executive Summary
Production tool calling requires strict Zod schema parsing, execution timeouts, and defensive output formatting to ensure AI agents handle tool errors without failing.
The Specific Challenge
In enterprise AI applications, tools give models the ability to query real-time data, execute dynamic code, and mutate application state. However, unstructured tool responses or malformed parameters frequently cause downstream agent failures.
Schema-First Tool Design
Every tool exposed to an AI model must have an unambiguous, strongly validated parameter contract.
import { z } from "zod";
export const SearchDatabaseSchema = z.object({
query: z.string().min(3, "Search query must be at least 3 characters"),
limit: z.number().int().min(1).max(50).default(10),
filterCategory: z.string().optional(),
});
export type SearchDatabaseInput = z.infer<typeof SearchDatabaseSchema>;Defensive Tool Execution Wrapper
async function safeToolExecute<T>(
toolName: string,
fn: () => Promise<T>
): Promise<{ success: boolean; data?: T; error?: string }> {
try {
const data = await fn();
return { success: true, data };
} catch (err: any) {
console.error(`[Tool Failure - ${toolName}]: ${err.message}`);
return { success: false, error: err.message };
}
}Key Trade-Off Matrix
Summary Recommendation
For enterprise production environments, implement strict Zod schema validation on tool inputs and wrap all tool handlers in defensive try-catch wrappers. Returning structured error messages to the model enables the agent to self-correct during subsequent loops.