# Node SDK

Reference for @percepteye/agent-flywheel: the framework API for any JavaScript or TypeScript agent, and the plugin for OpenClaw and DeepSeek Harness. Install, frameworks, modes, configuration and on-disk formats.

Node `>=22.22.3`. Zero runtime dependencies. MIT licensed. Source: [percepteye-ai/agent-flywheel-node](https://github.com/percepteye-ai/agent-flywheel-node).

## Install

```bash
npm install @percepteye/agent-flywheel
```

Node 22.22.3 or later. Zero runtime dependencies, and TypeScript declarations included. Then get the agent's key: sign up at [launch.percepteye.ai](https://launch.percepteye.ai/onboard/agent-sdk), name the agent and pick Node. The key is shown once and is scoped to that one agent.

```bash
export PERCEPTEYE_API_KEY=pefw_...        # from https://launch.percepteye.ai/onboard/agent-sdk
export PERCEPTEYE_AGENT_ID=support-agent  # the name you gave the agent there
```

Or get the key without leaving your terminal:

```bash
npx @percepteye/agent-flywheel login --agent-id support-agent
```

Without a key nothing leaves the machine: the SDK still records your agent's real tool-failure rate locally, with no account.

### Wrap each turn you serve

```typescript
import { flywheel } from "@percepteye/agent-flywheel";

const fw = flywheel({ agentId: "support-agent", framework: "ai-sdk" });

app.post("/chat", async (req, res) => {
  const answer = await fw.turn(req.body.message, (t) => runAgent(t), {
    conversationId: req.body.conversationId,
  });
  res.json({ answer });
});

process.on("SIGTERM", () => fw.close());   // sends what is waiting, bounded
```

`fw.turn(input, fn)` runs your agent and returns what it returns. It records the user's message, every tool call, and the answer. A person can approve a better system prompt and model for the agent, and `fn` is then handed both: `t.systemPrompt(yours)` returns the approved prompt (or yours), and `t.model` is the approved endpoint (or `null`, so keep yours). `runAgent` is your agent with three lines changed:

```typescript
import { generateText, stepCountIs, wrapLanguageModel } from "ai";
import { createOpenAI } from "@ai-sdk/openai";
import { flywheelMiddleware, flywheelTools } from "@percepteye/agent-flywheel/ai-sdk";
import type { FlywheelTurn } from "@percepteye/agent-flywheel";

async function runAgent(t: FlywheelTurn) {
  const provider = t.model
    ? createOpenAI({ baseURL: t.model.baseURL, apiKey: t.model.apiKey ?? undefined }).chat(t.model.model!)
    : createOpenAI().chat("gpt-4.1-mini");                                     // your own model
  const { text } = await generateText({
    model: wrapLanguageModel({ model: provider, middleware: flywheelMiddleware() }),
    system: t.systemPrompt("You are a support agent for Acme."),                 // yours, or the approved one
    tools: flywheelTools(tools),                                                 // records every call
    prompt: t.input,
    stopWhen: stepCountIs(8),
    abortSignal: t.signal,
  });
  return text;
}
```

### Let it practise

The same function runs the graded tasks the control plane writes for your agent from its real tools. Run it in a separate process pointed at a test or sandbox backend, because rollouts are real runs on your real tools:

```typescript
// PERCEPTEYE_AGENT_MODE=training node train.js
const fw = flywheel({ agentId: "support-agent", framework: "ai-sdk" });
await fw.serve(runAgent, { maxRollouts: 8 });
```

`fw.serve(fn)` claims one task at a time and hands it to `fn` as `t.input`. In a rollout, `t.model` is always set: it is the checkpoint being trained, served through that rollout's own gateway, and **your agent must call it**. A call that throws is reported as a failed attempt, never dropped. `t.signal` aborts if the control plane revokes the rollout or its deadline passes.

> **Important:**
> 
> Training takes two switches, one on each side. `PERCEPTEYE_AGENT_MODE=training` on this process says it may run practice tasks. **Allow practice runs** on the agent in Mission Control (an org admin) says the platform may send them. With either off, no practice task runs: `fw.serve()` refuses in production mode and names the switch.

## Frameworks

| Framework | What you add | Records tool outcomes | Describes the agent (prompt, tools, model) |
| --- | --- | --- | --- |
| **Vercel AI SDK** (`ai` 5, 6, 7) | `flywheelTools(tools)` and `flywheelMiddleware()` from `@percepteye/agent-flywheel/ai-sdk` | Yes | Yes |
| **LangChain.js / LangGraph.js** (`@langchain/core` 0.3, 1.x) | `callbacks: [flywheelCallbacks()]` from `@percepteye/agent-flywheel/langchain` | Yes | Yes |
| **OpenAI SDK, Anthropic SDK, Mastra, your own loop** | `t.recordToolCall({...})` where your tools run | Yes | With [`describe`](https://docs.percepteye.ai/sdk-node.md#describe) |
| **OpenClaw, DeepSeek Harness** | The plugin: [OpenClaw and DeepSeek Harness](https://docs.percepteye.ai/sdk-node.md#openclaw-and-deepseek-harness) | Yes | Prompt and model; tools with `describe` |

Neither adapter imports its framework: both are plain objects in the shapes the framework already accepts, so they add no dependency and no version pin.

**LangChain.js and LangGraph.js.** Create the handler inside the turn, so it is bound to that turn:

```typescript
import { ChatOpenAI } from "@langchain/openai";
import { createAgent } from "langchain";
import { flywheelCallbacks } from "@percepteye/agent-flywheel/langchain";

const answer = await fw.turn(message, async (t) => {
  const model = t.model
    ? new ChatOpenAI({ model: t.model.model!, apiKey: t.model.apiKey ?? undefined,
                       configuration: { baseURL: t.model.baseURL } })
    : new ChatOpenAI({ model: "gpt-4.1-mini" });
  const agent = createAgent({ model, tools, systemPrompt: t.systemPrompt("You are a support agent for Acme.") });
  const out = await agent.invoke({ messages: [{ role: "user", content: t.input }] },
                                 { callbacks: [flywheelCallbacks()], signal: t.signal });
  return String(out.messages.at(-1)?.content ?? "");
});
```

**Anything else.** Record each tool call where it runs. Say the outcome when you know it, because only you can know that an HTTP 200 was a permission denial:

```typescript
import { recordToolCall } from "@percepteye/agent-flywheel";

const started = performance.now();
const res = await fetch(`${API}/refunds`, { method: "POST", body: JSON.stringify({ orderId }) });
const reply = await res.json();
recordToolCall({
  name: "issue_refund", arguments: { orderId },
  outcome: reply.denied ? "failed" : "ok", statusCode: res.status,
  result: reply, latencyMs: performance.now() - started,
});
```

`recordToolCall` is a no-op outside a turn, so it can stay in code that also runs outside the flywheel. Leave `outcome` out and it is read from `error` and `result`: an error is `failed`, and a returned value is `unknown`, never `ok`.

## OpenClaw and DeepSeek Harness

The same package loads as a plugin into **OpenClaw** and **DeepSeek Harness**, where the harness fires the hooks and you write no code. Both uses share one set of lanes: the framework API is a small in-process host for the same plugin.

```bash
openclaw plugins install @percepteye/agent-flywheel          # OpenClaw
dsh plugin --profile <name> add @percepteye/agent-flywheel   # DeepSeek Harness
```

No import, no wrapper process, no sidecar. Start your agent the way you always did. Requires OpenClaw plugin API `>=2026.7.1`.

Without a key, the plugin records locally and sends nothing, forever. Add a key to start contributing:

```bash
export PERCEPTEYE_API_KEY=pefw_...
# or, from the terminal:
npx @percepteye/agent-flywheel login --agent-id my-agent
```

## Harness support

| Harness | Records outcomes | Practice tasks | Production capture | Applies approved prompt | Applies approved model |
| --- | --- | --- | --- | --- | --- |
| **OpenClaw** | `after_tool_call` | Training mode | Yes | `before_prompt_build` | `before_model_resolve` + a registered provider |
| **DeepSeek Harness** | `tools/result` | — | — | — | — |

Every recording hook is incapable of altering a run: OpenClaw's `after_tool_call` returns `void`, and dsh's `tools/result` is `@mode emit` over deep-frozen objects. Tool-result mutation seams are never used.

### OpenClaw

**Conversation access.** Set this in your OpenClaw config:

```jsonc
{ "plugins": { "entries": { "agent-flywheel": {
    "hooks": { "allowConversationAccess": true } } } } }
```

It enables three things OpenClaw classes as conversation hooks: reading your agent's answer (`agent_end`), describing your agent (`llm_input`), and pointing it at the approved model (`before_model_resolve`). Without it, the plugin refuses to claim practice tasks and says which capability is missing. Applying the approved prompt does not need it; `allowPromptInjection: false` blocks that half from the host side.

**Multiple agents on one host.** Set `agentId` to the agent this install serves. On a host running several agents with none named, the plugin applies nothing and says so, because guessing would swap a bystander's prompt.

**Practice tasks** need `mode: training` and **Allow practice runs** on in Mission Control; the runner says which switch is off. They run in a dedicated `percepteye-rollout-*` session, never one you are using. If the host closes that seam, the plugin falls back to delivering the task into the next turn you start.

**No `llm_call_count`.** OpenClaw's `llm_input` / `llm_output` hooks bracket a whole attempt, not each provider call, so the plugin reports no count rather than guess.

### DeepSeek Harness

Record-only in every mode. Outcomes are appended to `$PERCEPTEYE_TRAJECTORY_DIR/tool_calls.jsonl`; with that unset, nothing is written. DeepSeek Harness exposes no seam for handing a task to a running agent.

## Modes

|  | `training` | `production` (default) |
| --- | --- | --- |
| Records tool outcomes locally | Yes | Yes |
| Claims practice tasks, starts turns | Yes | No (hooks not registered) |
| Captures the turns you serve | n/a | Yes; uploads once an operator enables it |
| Applies the approved prompt and model | No | Yes |

Both halves apply only to this agent's own served turns: never a subagent, a scheduled turn, or another agent in the same gateway. See [Modes](https://docs.percepteye.ai/concepts-modes.md) and [Apply the approved model and prompt](https://docs.percepteye.ai/guides-apply-approved-policy.md).

## Configuration

Scalar settings go in your OpenClaw config under `plugins.entries.agent-flywheel.config`, or in the environment. **Config wins over the environment**, except for `introspect`, where an opt-out from either side stands.

| Config key | Environment variable | Default | What it decides |
| --- | --- | --- | --- |
| `apiKey` | `PERCEPTEYE_API_KEY` | None, then the key `login` stored for this `agentId` | The consent boundary. Without it nothing leaves the machine |
| `mode` | `PERCEPTEYE_AGENT_MODE` | `production` | Set `training` for practice tasks. An unreadable value degrades to `production` and says so |
| `agentId` | `PERCEPTEYE_AGENT_ID` | `openclaw` | The agent name reported, and whose approved pair is applied |
| `controlPlaneUrl` | `PERCEPTEYE_CONTROL_PLANE_URL` | `https://launch.percepteye.ai/api/flywheel/v1` | The control plane |
| `capture` | `PERCEPTEYE_CAPTURE` | On | `0`/`false`/`no`/`off` registers no hooks and records nothing |
| `applyPrompt` | `PERCEPTEYE_APPLY_PROMPT` | On | `0` subscribes no prompt hook and does not fetch the prompt |
| `applyModel` | `PERCEPTEYE_APPLY_MODEL` | On | `0` subscribes no model hook and registers no provider |
| `introspect` | `PERCEPTEYE_INTROSPECT` | On | `0` registers the agent with no description (your opt-out) |
| `retentionTurns` | — | None | Keep only the most recent N captured turns on disk |
| `describe` | — (config only) | None | The agent's `system_prompt`, `tools` and `model`, for what the plugin cannot observe |
| `rules` | — (config only) | None | Your Product's workflow rules. Unset sends nothing; `[]` retracts |
| `executionSnapshot` | — (config only) | None | Adapter-owned SHA-256 execution evidence. Missing or invalid evidence abstains |

Directories:

| Variable | Default | Holds |
| --- | --- | --- |
| `PERCEPTEYE_HOME` | `~/.percepteye` | Everything the package writes, including `credentials.json` |
| `PERCEPTEYE_CAPTURE_DIR` | `$PERCEPTEYE_HOME/captured` | Production turn capture |
| `PERCEPTEYE_TRAJECTORY_DIR` | Unset | Where a training run's outcomes are appended. Whoever runs the task sets it; the plugin only reads it |

### describe

OpenClaw's `llm_input` carries no tool list, so on its own the plugin registers an agent whose tools are unknown, and no task can be generated for it. Declare them:

```json
"plugins": { "entries": { "agent-flywheel": { "config": { "describe": {
  "system_prompt": "You triage support tickets.",
  "tools": [{ "name": "lookup_ticket", "description": "Read one ticket.",
              "input_schema": { "type": "object", "properties": { "id": { "type": "string" } } },
              "read_only": true }]
} } } } }
```

-   `tools` accepts OpenAI function specs, Anthropic tools, or `{name, description, input_schema, read_only}`.
-   `model` is a name or `{model_name, provider, temperature, max_tokens}`.
-   What you declare wins over what was observed. It anchors no execution identity.
-   A bad declaration is dropped with a warning; it never stops the plugin.

It is the Python SDK's `describe=`, read by the same rules.

### rules

```json
"plugins": { "entries": { "agent-flywheel": { "config": { "rules": [
  "Before issuing a refund, confirm the order number with the customer.",
  "Escalate to a human when the customer asks to close their account."
] } } } }
```

Declared once per start to `POST /agents/{id}/rulebook`, filed under the one Product that declares this agent, and used as training guidance. Never added to your served prompt. A refusal costs the rules and nothing else; read it as `rulebookRefusal` on the handle `register()` returns, or in the startup warning.

## What it writes

One JSON object per executed tool call, appended to `tool_calls.jsonl`, the same format the Python SDK writes:

```json
{"name":"exec","arguments":{"command":"exit 3"},"outcome":"failed","status_code":null,"error":"(Command exited with code 3)","error_class":"ToolError","latency_ms":155,"agent_name":"main","tool_call_id":"call_percepteye_1"}
```

### The training outbox

In training mode, each result the control plane has not acknowledged sits under `$PERCEPTEYE_HOME/outbox/<agentId>/`. It is written before the report is sent and removed when it lands; held results are delivered before the next claim.

| Suffix | Meaning |
| --- | --- |
| _(none)_ | Not recorded yet; retried before every claim |
| `.refused-lease-lost` | The lease is gone (what a `410` becomes) |
| `.refused-contract` | The SDK could not carry the payload |
| `.refused-400`, `-404`, `-409`, `-422` | Other final statuses |
| `.stale` | Aged out after seven days; kept |

A refused file is renamed, never deleted, and a warning names its path. `401`, `403` and unrecognized statuses keep retrying. No credential is ever written there.

## CLI

```bash
npx @percepteye/agent-flywheel login  [--agent-id ID] [--control-plane-url URL] [--no-browser]
npx @percepteye/agent-flywheel logout [--agent-id ID] [--control-plane-url URL]
```

`agentId` defaults to `openclaw`. After `login`, restart OpenClaw: the plugin reads the stored key when the gateway starts, whenever `PERCEPTEYE_API_KEY` and `apiKey` are unset.

## Package exports

| Import | Contents |
| --- | --- |
| `@percepteye/agent-flywheel` | The framework API (`flywheel`, `recordToolCall`, `currentTurn`, `ToolCallBlocked`) and the plugin (`register`) |
| `@percepteye/agent-flywheel/ai-sdk` | Vercel AI SDK adapter (`flywheelTools`, `flywheelMiddleware`) |
| `@percepteye/agent-flywheel/langchain` | LangChain.js and LangGraph.js adapter (`flywheelCallbacks`) |
| `@percepteye/agent-flywheel/customer-api` | `CustomerApiClient`, `CustomerRefusal` |
| `@percepteye/agent-flywheel/trajectory` | Trajectory reader |
| `@percepteye/agent-flywheel/classify` | Outcome classification |
| `@percepteye/agent-flywheel/dsh` | DeepSeek Harness integration |
| `@percepteye/agent-flywheel/tracing` | OpenTelemetry emission (`@opentelemetry/api` is an optional peer dependency) |

### Customer API client

```js
import { CustomerApiClient } from "@percepteye/agent-flywheel/customer-api";

const mc = new CustomerApiClient();               // mck_ key: apiKey or PERCEPTEYE_MC_API_KEY
const runs = await mc.listRuns({ productId: "prod_123", includePromptEvolutions: true });
const elig = await mc.trainingEligibility({ agentId: "agent_123" });
const sets = await mc.listEvalSets({ productId: "prod_123", limit: 20 });
const sec  = await mc.getHillclimbSecurity("run_123");
```

A refusal throws `CustomerRefusal` with `code`, `refusalMessage`, `customerMessage`, `customerWords`, `remediation`, `faultOwner`, `retryable`. Show `plainWords` to a person.

## Python or Node?

|  | Node SDK | Python SDK |
| --- | --- | --- |
| You have | A JavaScript or TypeScript agent, or an OpenClaw / DeepSeek Harness agent | A Python agent, or a CLI agent you launch yourself |
| You install | `npm install @percepteye/agent-flywheel` | `pip install agent-flywheel` |
| Production | `fw.turn(input, fn)` around each turn | `attach()` and a turn per answer |
| Training | `fw.serve(fn)` | `serve("module:factory")` |
| Adapters | Vercel AI SDK, LangChain.js / LangGraph.js | LangChain, LangGraph, Google ADK |
| The approved prompt and model | Handed to your agent as `t.systemPrompt()` and `t.model` (applied for you in a harness) | Handed back by `current_prompt()` and `apply_current_policy()` |

Pick one per agent, never both.

---
Source: https://docs.percepteye.ai/#sdk-node · Index: https://docs.percepteye.ai/llms.txt
