Skip to main content
Let the model call a multiplication function in your Node.js application. The program verifies the function call instead of trusting an answer that merely looks correct. The complete code is below. You can also browse the project on GitHub or open its JavaScript source.

1. Create the project

Use Node.js 22 or newer, npm, and an OpenAI API key with access to gpt-4.1-mini. Model requests use your provider account and can incur charges.
Keep npm optional dependencies enabled; they include the runtime for your platform. You do not need a separately installed runtime.

2. Configure the environment

Create a file named .env in this directory:
Replace the placeholder with your key. Add .env and node_modules/ to .gitignore. Existing shell environment values take precedence over this file. To use Anthropic instead, set COGNITIO_MODEL=anthropic/claude-sonnet-4-5 and ANTHROPIC_API_KEY to your Anthropic key; the code forwards either provider’s credential.

3. Create index.mjs

4. Run it

A successful run prints output like:
The assistant’s wording may vary. The program requires a real call with 6 and 7 and a product of 42; it throws if the model answers without calling the tool. Use a model with tool-calling support.

How it works

The descriptor is named multiply; direct Agent tools are exposed to the runtime with the sdk_ prefix. allowedTools: ["sdk_multiply"] makes this the only available tool. The input schema describes arguments to the model. It does not automatically validate callback input in v2. The callback checks that both values and their product are finite numbers before recording a result. For richer inputs, call a validator inside execute before any side effect. The function runs in your application process. An allowlist grant can settle permission before canUseTool; put mandatory checks inside the function or deterministic deny rules. See tools and permissions. This Agent owns its dedicated isolated runtime and closes it in finally. It allows three turns, uses a 90-second request deadline, and checks an estimated $0.03 budget. The budget is not a provider billing cap.

Use the existing project

If you prefer the complete GitHub project, copy that directory, run npm ci, copy .env.example to .env, set your key, and run npm start. On PowerShell, use Copy-Item .env.example .env; in a Unix shell, use cp .env.example .env.