Skip to main content
Send a fact, close its session handle, then resume the same transcript and ask the model to recall it. One client keeps the runtime alive through both turns. 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:
The session ID changes on each program run. The script checks that the resumed ID matches the first handle and that the answer mentions PostgreSQL. It throws if either check fails. The program makes two model requests.

How it works

session.close() releases the local handle and callbacks without normally deleting the server transcript. client.sessions.resume(sessionId) attaches a new handle while the same client and runtime remain alive. The application owns this low-level client and closes it in finally. Closing it removes the default isolated scratch state. Running the program again starts a new runtime; saving the session ID alone does not preserve the conversation across restarts. For cross-process persistence, use a persistent runtime or explicitly managed local state. Runtime policy and callback registrations must be restored after their process restarts. See sessions. Session.send() accepts the prompt only. To cancel an active session turn from your application, call session.interrupt() on that handle. The session permits one assistant turn per request and uses an estimated $0.03 budget; provider charges depend on actual usage.

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.