跪拜 Guibai
← All articles
Artificial Intelligence · Full Stack

Who Actually Executes Agent Tools? Inside Function Calling, MCP, and Pi's Harness

By Rater小宇宙 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Tool calling underpins every Agent product, so understanding that the Harness, not the model, is the executor clarifies where to add safety checks, error handling, and control. This matters for anyone building agents in production, where a native `tool.execute()` is trivial but stable, secure, and controllable execution is the real engineering burden.

Summary

A common misconception about Agent tool calling is that the large language model executes the tools. In reality, the LLM only decides which tool to call and with what parameters; the Harness, the engineering layer built from program code, is what finds the tool, validates arguments, runs it, and wraps the result back into a message for the model's next turn. This split between decision and execution is the core of the Agent = LLM + Harness formula.

The Pi framework's implementation shows the full lifecycle: `executeToolCalls()` chooses between sequential and parallel modes, then each call goes through `prepareToolCall()` which checks tool existence, adapts parameters, validates schemas, and runs a `beforeToolCall` hook for permission checks, user confirmation, logging, or policy interception. After `tool.execute()`, `afterToolCall` hooks handle desensitization, truncation, and error correction, and the result becomes a `ToolResultMessage` added to context.

A notable design choice is error handling: Pi converts tool exceptions into `isError: true` result messages instead of throwing them out of the loop, so the model can retry or adjust. The author draws three design principles: everything in the model interaction layer is a Message, hooks matter as much as the tool itself, and the clean separation between LLM decision-making and Harness execution is what makes Agent systems reliable.

Takeaways
— Agent = LLM + Harness; the LLM decides which tool to call and with what arguments, while the Harness actually executes the tool and returns results to the model.
— Function Calling is the mechanism by which an LLM expresses intent to call an external tool; MCP is a separate standard for discovering and accessing external tools, and the two complement each other.
— Pi executes tools in either sequential mode (awaiting each in a for loop) or parallel mode (collecting async functions and starting them with Promise.all), defaulting to parallel when no sequential requirement exists.
— Before execution, Pi checks that the tool exists, adapts parameters, validates schema, and runs a beforeToolCall hook that can intercept execution based on permission, user confirmation, audit, or policy rules.
— After execution, Pi runs afterToolCall hooks for desensitization, truncation, format conversion, error correction, or statistics.
— Tool execution results are wrapped as a ToolResultMessage and added to the conversation context, becoming part of the next LLM input.
— Pi converts tool exceptions into an isError: true ToolResultMessage rather than throwing them out of the Agent Loop, allowing the model to correct or retry the call.
— The core design philosophy: everything in the model interaction layer is a Message, hooks are as critical as the tool code itself, and separating LLM decision-making from Harness execution is essential for reliable Agent systems.
Conclusions

The emphasis on beforeToolCall and afterToolCall hooks shows that production Agent reliability comes from engineering control around the naive `tool.execute()`, not from the model itself.

Converting errors into result messages instead of exceptions is a deliberate pattern that treats model self-correction as a first-class recovery strategy, which is fine for small loops but could mask systemic failures.

The parallel-by-default behavior of Pi is a pragmatic efficiency choice, but it requires that tool calls be independent; sequential mode exists for operations that must happen in order.

The article's clear separation of LLM and Harness roles makes it easier to debug why an agent failed: either the model chose poorly or the harness mis-executed, which are distinct failure domains.

Concepts & terms
Harness
The engineering-side layer of an Agent that runs the code to find, validate, execute, and post-process tool calls, as opposed to the LLM which only decides what to do.
Function Calling
A mechanism that lets an LLM request external tool execution by generating a structured call with a tool name and parameters; it does not execute the tool itself.
MCP (Model Context Protocol)
A standardized protocol for connecting AI applications to external tools and data sources, allowing tools to be discovered and accessed uniformly, separate from the model's function calling.
ToolResultMessage
A message type in Pi that wraps the result of a tool execution, including an error flag `isError` when execution fails, and is added to the conversation context for the model to continue.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗