# fx

> Run fx agents on AnyRouter with the provider plugin. Install it, set one key, and pass anyrouter() to createFxAgent.


# fx

[fx](https://fx.sh) is an agent runtime from Vercel Labs ([source](https://github.com/vercel-labs/fx)). The [AnyRouter provider plugin](https://github.com/AnyRouterHQ/fx-provider-anyrouter) lets an fx agent run on any AnyRouter model with one key.

## Set up

The plugin is not on npm. Install it from GitHub, with `libfx` (`^0.0.16`) as its peer dependency:

```bash
npm install libfx github:AnyRouterHQ/fx-provider-anyrouter#v0.1.0
```

Set your key, from the [dashboard](https://anyrouter.dev/dashboard/keys):

```bash
export ANYROUTER_API_KEY=sk-ar-v1-...
```

Pass `anyrouter()` as the provider:

```ts
import { createFxAgent } from "libfx";
import { anyrouter, anyrouterCredentials } from "fx-provider-anyrouter";

const agent = createFxAgent({
  provider: anyrouter(),
  model: "anyrouter/hermes",
  credentials: anyrouterCredentials(),
});

for await (const event of agent.session().prompt("Hello")) {
  if (event.type === "text_delta") process.stdout.write(event.delta);
}
```

`model` must be an AnyRouter id. fx's default model will not work with a custom provider.

## Newer models

`anyrouter()` ships with a snapshot of 88 models. To use the live catalog instead:

```ts
import { fetchAnyRouterProvider } from "fx-provider-anyrouter";

const provider = await fetchAnyRouterProvider();
```

`fetchAnyRouterModels()` returns just the models record.

## Without the plugin

fx accepts a plain custom provider:

```ts
createFxAgent({
  provider: {
    name: "anyrouter",
    baseUrl: "https://anyrouter.dev/api/v1",
    models: { "anyrouter/hermes": { contextWindow: 1050000, maxOutputTokens: 64000, supportsToolUse: true } },
  },
  model: "anyrouter/hermes",
  credentials: async () => ({ apiKey: process.env.ANYROUTER_API_KEY! }),
});
```

## Troubleshooting

| Symptom | Fix |
|---|---|
| `401` | Check that `ANYROUTER_API_KEY` is set in the process that runs the turn, and that the key is valid. |
| Unknown model id | Use the `owner/model` form from the [models page](https://anyrouter.dev/models), for example `anyrouter/hermes`. |
| A new model is missing from the bundled list | Use `fetchAnyRouterProvider()` or `fetchAnyRouterModels()`. |
