Skip to Content
Tools SystemexecuteTools

executeTools

Problem

When your LLM returns multiple tool calls in a single response, you have to loop through each one, find the matching tool, run it, catch individual errors, and collect all results into a structured list. That is error-prone boilerplate that breaks differently depending on where you write it.


Solution

executeTools takes an array of tool call requests from the LLM and runs each one against the registry. It handles missing tools, catches execution errors per-call, and returns a typed result array with a success flag on each item — so you know exactly what worked and what did not.


Feature & Use-Case

Use executeTools when:

  • Your LLM provider returns multiple tool calls in one response
  • You need structured results — success/failure per tool, not a thrown exception
  • You are building a custom agent loop and need batch tool execution with error isolation

Import

import { executeTools } from "llm-layer-engine";

Function Signature

async function executeTools(input: ExecuteToolsInput): Promise<ToolExecutionResult[]>

Input Type

type ExecuteToolsInput = { toolCalls: ToolCall[]; }; type ToolCall = { name: string; input: Record<string, unknown>; id?: string; };

Return Type

type ToolExecutionResult = | { name: string; success: true; result: unknown } | { name: string; success: false; error: string };

Parameters

ParameterTypeRequiredDescription
input.toolCallsToolCall[]Array of tool calls returned by the LLM

Example — Execute Multiple Tool Calls

import { executeTools, registerTool } from "llm-layer-engine"; // Register tools first registerTool({ name: "get_weather", execute: async ({ city }) => ({ city, temp: "30°C" }), }); registerTool({ name: "get_time", execute: async ({ timezone }) => ({ timezone, time: "14:30 PKT" }), }); // Simulate tool calls from an LLM response const toolCalls = [ { name: "get_weather", input: { city: "Karachi" } }, { name: "get_time", input: { timezone: "Asia/Karachi" } }, { name: "unknown_tool", input: {} }, // This will fail gracefully ]; const results = await executeTools({ toolCalls }); results.forEach((r) => { if (r.success) { console.log(`✅ ${r.name}:`, r.result); } else { console.log(`❌ ${r.name}:`, r.error); } }); // ✅ get_weather: { city: "Karachi", temp: "30°C" } // ✅ get_time: { timezone: "Asia/Karachi", time: "14:30 PKT" } // ❌ unknown_tool: Tool unknown_tool not found

Error Handling Behavior

ScenarioWhat executeTools Does
Tool name not in registryReturns { success: false, error: "Tool X not found" }
Tool throws during executionReturns { success: false, error: err.message }
Tool executes successfullyReturns { success: true, result: <output> }

No individual failure stops the other tools from running. Every call in the batch completes independently.


Conclusion

executeTools is your batch tool runner. Pass in all tool calls from your LLM response and get back a clean result array. It isolates failures so one broken tool does not crash your agent loop. Pair it with registerTool and reActLoop for a complete agent setup.

Last updated on