Many agent tutorials jump straight into large orchestration frameworks, which can hide what is actually happening between the model and your application. At its core, tool calling is a simple loop: you send the model your messages along with JSON Schema descriptions of available functions; the model returns structured data naming a tool and its arguments; your JavaScript executes that function, such as a fetch request, database query, or local file read; then you append the result to the conversation with the matching tool-use ID and ask the model to continue. I put together a small Node.js example that demonstrates the full process, including details that often cause problems: returning an error result when a tool fails, handling multiple tool calls in one response, and accounting for how token usage compounds across repeated runs. There is also an interactive browser demo using a mock filesystem, so the loop can be tested without setting up a local environment. How are others handling client-side tool execution, large tool outputs, and context management in production?
1 Answer
That basic loop really is the foundation of an agent, even though production systems add quite a bit around it. One useful pattern is returning a handle or reference for large outputs instead of inserting the entire result into the conversation. It also helps to enforce a gate that prevents the model from producing a final answer until the tool result has actually been processed. Those safeguards belong in the runtime rather than relying only on instructions in the system prompt.

Good point about keeping those checks in the runtime. The artifact-handle approach is especially useful once tools start returning files, logs, or large query results instead of small JSON payloads.