Skip to main content
Ask one question and print the answer. Each run starts a fresh conversation. 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 a short answer, for example:
The wording varies by model and run. The script rejects unsuccessful results and empty answers. Change the string passed to agent.run() to ask a different question.

How it works

disallowedTools: ["*"] makes this a text-only agent. An empty allowedTools list would leave tools unrestricted. The example omits environment details from the prompt and does not load user or project instructions. The explicit spawn configuration gives this Agent its own isolated runtime. agent.close() in finally stops it even when the request fails. The request has a 90-second cancellation deadline and a one-turn limit. The estimated $0.03 budget is checked using reported usage; it is not a prepaid spending cap. For streamed output, see streaming and cancellation. To keep context between messages, use the conversation recipe.

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.