Node.js

The Node.js entry point runs the agent through a native addon, with WebAssembly as a fallback.

Install

Use Node.js 20.19 or later in the 20.x line, or Node.js 22.12 or later (^20.19.0 || >=22.12.0):

npm install libfx@0.0.18

Bun runs libfx as Node.js does. For the WebAssembly backend, use Bun 1.4.2 or later.

Follow Quick start to connect a model and run your first prompt.

Choose an entry point

ImportDescriptionEnvironment
libfxThe entry point for the current environmentNode or browser
libfx/nodeNative addon first, WebAssembly fallbackNode
libfx/browserWebAssemblyBrowser
libfx/wasmThe engine and terminal on WebAssembly, without packaged asset pathsNode or browser
libfx/durable-local, libfx/durable-vercel, libfx/durable-worldWhere sessions liveNode
libfx/chatgpt, libfx/grokSign-in for ChatGPT and Grok plansNode
libfx/mcp, libfx/skills, libfx/skills/nodeMCP and skills adaptersNode or browser; skills/node in Node or Bun

Node applications can also use require('libfx'). With TypeScript's moduleResolution: 'bundler', add 'node' to customConditions to get the Node.js types.

Choose a backend

backend: 'auto' is the default. It tries the native addon first and loads the WebAssembly backend only when native execution is unavailable. Set backend: 'native' when you want the agent to fail if the addon is unavailable, or backend: 'wasm' to use WebAssembly only.

libfx has native addons for Linux and macOS on x64 and arm64, one package each. libfx lists them as optional dependencies, so your package manager installs only the package for your platform:

PlatformPackage
macOS on arm64@libfx/libfx-darwin-arm64
macOS on x64@libfx/libfx-darwin-x64
Linux on arm64@libfx/libfx-linux-arm64-gnu
Linux on x64@libfx/libfx-linux-x64-gnu

Keep optional dependencies enabled when installing or deploying with the native backend. Linux addons need glibc 2.34 or later. Windows uses the WebAssembly fallback. The native backend doesn't need JSPI or experimental Node flags; every WebAssembly path needs JSPI. On the native backend, calls from one model response can run at the same time, as Run calls together describes.

The packaged native addon implements the agent's engine, so createFxTerminal() uses WebAssembly. See the WebAssembly terminal for that API and AgentOptions for supplying your own addon or WebAssembly asset.

Use the agent in a server route

Create the agent once at module scope, and use it from every route:

// lib/agent.js
import { createFxAgent } from 'libfx'

export const agent = createFxAgent({ model: 'anthropic/claude-opus-5.5', tools })

Then return a session's stream from a route, as Prompts and streaming shows. Don't create or close an agent per request: the agent keeps running a turn after the response ends. On Vercel, add the queue route from Durability.

Native agents support Next.js 15 with webpack and Next.js 16 with webpack or Turbopack, including standalone builds. The matching optional platform package supplies the native asset, so you don't need serverExternalPackages or manual file tracing. Include that package in the deployment, and build on the platform you deploy to: a standalone build made on macOS carries only the macOS addon, so it can't use the native backend on Linux. If you use WebAssembly in a standalone build, supply its assets yourself, because the standalone tracer leaves out .wasm files.

Handle a missing backend

import { getBackendInfo } from 'libfx'

const info = await getBackendInfo({ surface: 'agent', backend: 'auto' })
console.log(info.backend)
console.log(info.attempts)

getBackendInfo() checks whether the native addon or WebAssembly can load. It doesn't validate credentials or make a model request, though a remote WebAssembly asset may still need a fetch. Use surface: 'terminal' to check terminal support.

When the package for your platform isn't installed, for example because optional dependencies were omitted or the lockfile came from another platform, backend: 'auto' uses WebAssembly instead. getBackendInfo() reports the native attempt as LIBFX_NATIVE_ARTIFACT_MISSING, with a message that names the package, such as the native addon package @libfx/libfx-linux-x64-gnu for linux-x64 is not installed. Install that package, or reinstall without --omit=optional, to use the native backend.

LIBFX_JSPI_REQUIRED means the WebAssembly path needs JSPI. If the platform package is missing, the error names it. On Node versions that need a flag, start with node --experimental-wasm-jspi agent.mjs. With backend: 'native', LIBFX_NATIVE_UNAVAILABLE means the native addon can't provide the requested agent or terminal. The attempts from getBackendInfo() tell missing files, unsupported platforms, and other loading failures apart.

Migrating from 0.0.17

libfx 0.0.18 has no API changes. Update the version in package.json to 0.0.18.

Migrating from 0.0.16

  • delegation events no longer report a yielded phase. A turn that yieldAt stops still resolves its engine turn with stopReason: 'yielded', but a session's TurnResult and turn_end event never report it. See API reference.

Migrating from 0.0.15

  • Update Node.js to ^20.19.0 || >=22.12.0. This requirement also applies to require('libfx').
  • The native addons moved out of libfx into one optional package per platform, so an install is now less than half the size of 0.0.15's. Keep optional dependencies enabled so the addon for your platform is installed. Without it, the default backend falls back to WebAssembly, which requires JSPI. A standalone build carries only the addon for the platform it was built on. See Choose a backend.
  • Upgrade every worker sharing a session store before starting new turns. Saved turns with context updates use journal and snapshot version 4, which libfx 0.0.15 cannot read. libfx 0.0.16 reads earlier formats. See Save and restore.

Migrating from 0.0.14

  • Engines using provider: 'codex' or 'grok' require an explicit model from that provider. Durable agents can set it on the agent or each session. See Providers.
  • prompt() rejects empty prompts and invalid image input before a durable session queues or saves it. With resizeImage, image content and size checks apply to its output when the turn runs. See Images.
  • Earlier libfx versions can't open journals or snapshots that use the new saved-image references. libfx 0.0.15 still opens sessions from earlier versions.

Migrating from 0.0.13

In libfx 0.0.13 and earlier, createFxAgent() created one in-memory conversation. That's now createFxEngine(), and createFxAgent() creates the durable agent:

  • To keep the old behavior, rename await createFxAgent(options) to await createFxEngine(options).
  • To move to sessions, call createFxAgent(options) without await, then agent.session().prompt().

Code written for 0.0.13 runs against the agent once its tools set idempotent, with a few differences: breaking out of a turn's events no longer cancels it, turns have no cancel() or steer() (the session does), and persistence and sessionId throw because the durability holds the session.

Three changes can break existing code:

  • Every tool given to createFxAgent() must set idempotent to true or false. Without it, createFxAgent() throws an error that names the tool. See Tools with effects.
  • A tool's writes now defaults to the opposite of idempotent, in createFxEngine() too, so a tool that isn't marked idempotent runs alone.
  • A journal written by 0.0.14 can't be loaded by an older libfx, which fails with FxJournalVersionError.

Migrating from 0.0.7

The agent API changed in 0.0.8, and again in 0.0.14. These steps lead to the 0.0.13 API; then follow Migrating from 0.0.13:

  • Pass apiKey and model directly instead of through env.
  • Call agent.prompt() instead of agent.createSession() followed by session.prompt().
  • Save conversation checkpoints, and supply tools and instructions again when you restore.

For the old API, use the 0.0.7 SDK README.

Agent guides