---
title: fx
description: Learn how to use the fx harness adapter.
url: "https://ai-sdk.dev/providers/ai-sdk-harnesses/fx"
docs_index: /llms.txt
---

> For an index of all documentation, see [/llms.txt](/llms.txt).

The fx harness adapter connects `HarnessAgent` to [fx](https://fx.sh) using
[libfx](https://www.npmjs.com/package/libfx). The runtime runs on the host,
while built-in filesystem and shell tools operate exclusively on the sandbox.
No bridge process runs inside the sandbox.

Harness packages are **experimental**. Expect breaking changes between
releases as this early API gets further refined.

## Setup

```bash
pnpm add @ai-sdk/harness @ai-sdk/harness-fx @ai-sdk/sandbox-vercel
```

libfx is installed with the adapter. No fx executable is downloaded or
installed inside the sandbox when a session starts.

## Import

```ts
import { createFx, fx } from '@ai-sdk/harness-fx';
```

`fx` is equivalent to `createFx()` with its default configuration.

## Basic Usage

```ts
import { HarnessAgent } from '@ai-sdk/harness/agent';
import { fx } from '@ai-sdk/harness-fx';
import { createVercelNetworkSandboxSession } from '@ai-sdk/sandbox-vercel';

const agent = new HarnessAgent({
  harness: fx,
  model: 'openai/gpt-5.6-luna',
});

const sandboxSession = await createVercelNetworkSandboxSession({
  runtime: 'node24',
  template: await agent.getSandboxTemplate(),
});
const session = await agent.createSession({ sandboxSession });

let exitCode = 0;
try {
  const result = await agent.stream({
    session,
    prompt: 'Check the test failures and fix the production code.',
  });

  for await (const part of result.stream) {
    if (part.type === 'text-delta') {
      process.stdout.write(part.text);
    }
  }
} catch (err) {
  exitCode = 1;
  console.error(err);
} finally {
  await session.destroy();
  await sandboxSession.destroy();
  process.exit(exitCode);
}
```

## Adapter Settings

Use `createFx()` to configure the runtime:

```ts
const harness = createFx({
  auth: 'ai-gateway',
  effort: 'low',
  serviceTier: 'default',
});
```

Settings:

- `auth`: selects `auto`, `direct`, or `ai-gateway` authentication, or accepts
  an authentication environment for programmatically resolved credentials.
  `auto` prefers Gateway credentials, `direct` prefers subscription credentials
  with Gateway fallback, and `ai-gateway` uses Gateway only.
- `mcpServers`: MCP server definitions keyed by server name. Supports HTTP and
  SSE servers, with `url`, optional `type`, `headers`, and `disabled` fields.
  Connections run on the host; command/stdio servers are unsupported.
- `headers`: extra headers sent with AI Gateway requests from the host.
- `effort`: reasoning effort for models that support it.
- `serviceTier`: selects `default`, `fast`, or `ultrafast`. `default` is
  equivalent to omitting the setting and leaves libfx's defaults unchanged.
  Fast and Ultra require a model that supports the selected tier.
- `backend`: libfx runtime backend (`auto`, `native`, or `wasm`). The default
  is `auto`, which prefers the native backend. The WASM backend requires
  JavaScript Promise Integration (JSPI) support.

## Authentication

fx supports [Vercel AI Gateway authentication](https://fx.sh/docs/getting-started/authentication)
and Codex or Grok subscriptions. For Gateway authentication, set one of these
environment variables:

- `VERCEL_OIDC_TOKEN`
- `AI_GATEWAY_API_KEY`

The adapter prefers `AI_GATEWAY_API_KEY` when both are available.
`AI_GATEWAY_BASE_URL` optionally overrides the Gateway endpoint. Credentials
are used on the host, not forwarded into the sandbox.

When Gateway credentials are absent, `auto` discovers subscription credentials
in this order:

1. fx Codex: `~/.fx/chatgpt-auth.json`.
2. fx Grok: `~/.fx/grok-auth.json`.
3. Native Codex: the credential store under `CODEX_HOME` or `~/.codex`,
   respecting `cli_auth_credentials_store` and the native keyring settings.
4. Native Grok: the xAI OAuth record in `GROK_HOME/auth.json` or `~/.grok/auth.json`.

libfx refreshes subscription tokens near expiry and after an authentication
rejection. The adapter saves rotated credentials back to the selected store.
Failures after selection do not silently switch subscriptions or fall back to
Gateway. Credentials are never included in persisted lifecycle state.

Pass an authentication environment when the host resolves credentials at
runtime instead of exposing them through `process.env`:

```ts
const gatewayHarness = createFx({
  auth: {
    AI_GATEWAY_API_KEY: await resolveGatewayToken(),
    AI_GATEWAY_BASE_URL: 'https://ai-gateway.vercel.sh',
  },
});
```

The supplied record replaces the host environment for authentication
discovery. The adapter does not add its credentials to `process.env` or include
their values in persisted lifecycle state.

Use `direct` to try subscriptions before Gateway, or `ai-gateway` to disable
subscription discovery:

```ts
const directHarness = createFx({ auth: 'direct' });
const gatewayHarness = createFx({ auth: 'ai-gateway' });
```

An explicit authentication environment does not inspect ambient subscription
stores. Resumed sessions keep their original provider and require compatible
credentials for that provider.

## Sandbox

fx runs on the host and accepts either a restricted sandbox session or a
network sandbox session. No exposed sandbox ports are required:

```ts
const sandboxSession = await createVercelNetworkSandboxSession({
  runtime: 'node24',
  template: await agent.getSandboxTemplate(),
});
```

The host needs network access for AI Gateway and configured MCP servers.
The sandbox does not need network access for model requests. Built-in tools
use the sandbox's file and command-execution APIs; the sandbox must provide
Bash and GNU command-line utilities such as `find`, `grep`, `realpath`,
and `base64`.

## Built-in Tools

The adapter exposes `read`, `write`, `edit`, `bash`, `grep`, `glob`, `ls`, and
`askUserQuestions`. Filesystem and shell operations run in the session sandbox.
The adapter does not expose the fx CLI's other built-in tools.

Built-in tool filtering is supported through `activeTools` and `inactiveTools`.
With `permissionMode: 'allow-reads'`, file edits and shell commands require
approval. With `allow-edits`, shell commands require approval, as do `write`
and `edit` calls that touch `.agents/skills` or `.fx` in the session workdir.
These paths hold configuration and skills that can run code or change the
agent's behavior in later sessions. A path matches when it is a protected path,
lies inside one, or is a parent of one. Symlinks are resolved in both
directions: writes through an alias that points into a protected path match, as
do writes to targets of symlinks inside protected directories. While a
protected path exists, writes to hard-linked files also require approval.
`allow-all` runs these tools without approval requests.

## Session Lifecycle

The lifecycle state carries the fx session itself: the libfx checkpoint of the
conversation and, for an unfinished turn, its replay records, including model
responses, tool approvals and host tool results. Nothing about the session is
stored in the sandbox, so code running in the sandbox cannot read or change it.
A future process restores the session from the lifecycle state after the
caller reattaches the sandbox.

Suspending a turn, detaching and stopping produce a lifecycle state. A process
that exits without one of them resumes from the last lifecycle state the caller
saved.

## Session Storage

The libfx checkpoint can reach several MiB, and an unfinished turn adds its
model responses, so the lifecycle state can become large. To keep it small,
pass a `sessionStorage` that keeps the session in an append-only log somewhere
else, such as an object store, a database table or a stream. When a session
suspends a turn, detaches or stops, the harness appends one entry holding what
changed since the previous entry, and the lifecycle state holds only `leafId`,
the id of that entry. Restoring a session reads the log and continues from that
entry. Each entry's `data` is opaque JSON; the harness checks each entry when
it reads it.

```ts
import { createFx, type FxSessionStorage } from '@ai-sdk/harness-fx';

const storage: FxSessionStorage = {
  append: async ({ sessionId, entry }) => {
    await db
      .insert(fxSessionEntries)
      .values({ sessionId, ...entry })
      .onConflictDoNothing();
  },
  read: async ({ sessionId }) =>
    db
      .select()
      .from(fxSessionEntries)
      .where(eq(fxSessionEntries.sessionId, sessionId)),
};

const harness = createFx({ sessionStorage: storage });
```

Entries are never changed or deleted. Every entry points to the entry before
it, its `parentEntryId`. A second turn resumed from the same state appends a new
branch, and each lifecycle state's `leafId` selects its own branch. An entry
whose `entryId` the log already holds is a retried append, so the example keeps
`(sessionId, entryId)` unique and ignores the conflict. `read` may return every
entry of the session, in any order, as above. To read less, return only the
entries that lead to `leafId`: the entry whose `entryId` is `leafId`, then its
`parentEntryId`, and so on.

A lifecycle state recorded with a session storage holds a `leafId` and only
restores with a `sessionStorage`. A lifecycle state recorded without one holds
the whole session and only restores without a `sessionStorage`.

## Known Limitations

- Manual compaction is unsupported by stable libfx and throws
  `HarnessCapabilityUnsupportedError`.

## Related

- [HarnessAgent](/docs/ai-sdk-harnesses/harness-agent)
- [Harness tools](/docs/ai-sdk-harnesses/tools)
- [Harness adapters](/docs/ai-sdk-harnesses/harness-adapters)

---

For a semantic overview of all documentation, see [/sitemap.md](/sitemap.md)

For an index of all available documentation, see [/llms.txt](/llms.txt)

For agent-facing discovery, including API and MCP surfaces, see [/agents.md](/agents.md)