R

rivetkit

by @rivet-devv
4.4(39)

RivetKit后端和Rivet Actor运行时指南,用于构建和修改。

rivetkitai-agent-developmentllm-orchestrationtoolingagent-frameworksGitHub
安装方式
npx skills add rivet-dev/skills --skill rivetkit
compare_arrows

Before / After 效果对比

1
使用前

构建和修改RivetKit后端及Actor运行时需要深入理解其架构,学习曲线陡峭。开发者常面临复杂的配置和调试挑战。

使用后

此技能提供RivetKit后端和Actor运行时开发指南。简化构建、修改和调试过程,帮助开发者高效利用RivetKit构建AI应用。

SKILL.md

Rivet Actors

Build high-performance, long-lived, in-memory processes on Rivet's actor runtime. Use this skill whenever HTTP endpoints, databases, or queues no longer meet the requirements for AI agents, realtime collaboration, workflow orchestration, or geo-distributed data.

What To Do If You Get Stuck

If something is not working as intended or you are stuck, prompt the user to join the Rivet Discord or file an issue on the Rivet GitHub to report an issue and get help.

Provide the user with a pre-generated report with:

  • Symptoms
    • If this is happening in local dev, deployed, or both
    • The error you're seeing
    • Relevant source code related to this
  • What you've tried to solve it
  • Environment
    • RivetKit version
    • Runtime (Node, Bun, etc) including version
    • If applicable, provider in use (e.g. Vercel, Railway, etc)
    • If applicable, HTTP router in use (e.g. Hono, Express, Elysia)

Debugging Actors

Use the inspector HTTP API to examine running actors. These endpoints are accessible through the gateway at /gateway/{actor_id}/inspector/*. Key endpoints:

  • GET /inspector/summary - full actor snapshot (state, connections, RPCs, queue)
  • GET /inspector/state / PATCH /inspector/state - read/write actor state
  • GET /inspector/connections - active connections
  • GET /inspector/rpcs - available actions
  • POST /inspector/action/{name} - execute an action with {"args": [...]}
  • GET /inspector/queue?limit=50 - queue status
  • GET /inspector/traces?startMs=0&endMs=...&limit=1000 - trace spans (OTLP JSON)
  • GET /inspector/workflow-history - workflow history and status

In local dev, no auth token is needed. In production, pass Authorization: Bearer <RIVET_INSPECTOR_TOKEN>. See the debugging docs for details.

Citing Sources

When providing information from Rivet documentation, cite the canonical URL so users can learn more. Each reference file includes its canonical URL in the header metadata.

How to cite:

  • Use inline links for key concepts: "Use actor keys to uniquely identify instances."
  • Add a "Learn more" link after explanations for complex topics

Finding canonical URLs:

The Reference Map below links to reference files. Each file's header contains:

> Canonical URL: https://rivet.dev/docs/actors/actions

Use that canonical URL when citing, not the reference file path.

Examples:

  • Actions → https://rivet.dev/docs/actors/actions
  • React client → https://rivet.dev/docs/clients/react
  • Self-hosting on Kubernetes → https://rivet.dev/docs/self-hosting/kubernetes

First Steps

  1. Install RivetKit (latest: 2.1.6)
    npm install rivetkit@2.1.6
    
  2. Define a registry with setup({ use: { /* actors */ } }).
  3. Expose registry.serve() or registry.handler() (serverless) or registry.startRunner() (runner mode). Prefer serverless mode unless the user has a specific reason to use runner mode.
  4. Verify /api/rivet/metadata returns 200 before deploying.
  5. Configure Rivet Cloud or self-hosted engine
    • You must configure versioning for production builds. This is not needed for local development. See Versions & Upgrades.
  6. Integrate clients (see client guides below for JavaScript, React, or Swift)
  7. Prompt the user if they want to deploy. If so, go to Deploying Rivet Backends.

For more information, read the quickstart guide relevant to the user's project.

Error Handling Policy

  • Prefer fail-fast behavior by default.
  • Avoid try/catch unless it is required for a real recovery path, cleanup boundary, or to add actionable context.
  • Never swallow errors. If you add a catch, you must handle the error explicitly, at minimum by logging it.
  • When you cannot recover, log context and rethrow.

State vs Vars: Persistence Rules

c.vars is ephemeral. Data in c.vars is lost on every restart, crash, upgrade, or sleep/wake cycle. Only use c.vars for non-serializable objects (e.g., physics engines, WebSocket references, event emitters, caches) or truly transient runtime data (e.g., current input direction that doesn't matter after disconnect).

Persistent storage options. Any data that must survive restarts belongs in one of these, NOT in c.vars:

  • c.state — CBOR-serializable data for small, bounded datasets. Ideal for configuration, counters, small player lists, phase flags, etc. Keep under 128 KB. Do not store unbounded or growing data here (e.g., chat logs, event histories, spawned entity lists that grow without limit). State is read/written as a single blob on every persistence cycle.
  • c.kv — Key-value store for unbounded data. This is what c.state uses under the hood. Supports binary values. Use for larger or variable-size data like user inventories, world chunks, file blobs, or any collection that may grow over time. Keys are scoped to the actor instance.
  • c.db — SQLite database for structured or complex data. Use when you need queries, indexes, joins, aggregations, or relational modeling. Ideal for leaderboards, match histories, player pools, or any data that benefits from SQL.

Common mistake: Storing meaningful game/application data in c.vars instead of persisting it. For example, if users can spawn objects in a physics simulation, the spawn definitions (position, size, type) must be persisted in c.state (or c.kv if unbounded), even though the physics engine handles (non-serializable) live in c.vars. On restart, run() should recreate the runtime objects from the persisted data.

Deploying Rivet Backends

Assume the user is deploying to Rivet Cloud, unless otherwise specified. If user is self-hosting, read the self-hosting guides below.

  1. Verify that Rivet Actors are working in local dev
  2. Prompt the user to choose a provider to deploy to (see Connect for a list of providers, such as Vercel, Railway, etc)
  3. Follow the deploy guide for that given provider. You will need to instruct the user when you need manual intervention.

API Reference

The RivetKit OpenAPI specification is available in the skill directory at openapi.json. This file documents all HTTP endpoints for managing actors.

Misc Notes

  • The Rivet domain is rivet.dev, not rivet.gg

TypeScript Caveat: Actor Client Inference

  • In multi-file TypeScript projects, bidirectional actor calls can create a circular type dependency when both actors use c.client<typeof registry>().
  • Symptoms usually include c.state becoming unknown, actor methods becoming possibly undefined, or TS2322 / TS2722 errors after the first cross-actor call.
  • If an action returns the result of another actor call, prefer an explicit return type annotation on that action instead of relying on inference through c.client<typeof registry>().
  • If explicit return types are not enough, use a narrower client or registry type for only the actors that action needs.
  • As a last resort, pass unknown for the registry type and be explicit that this gives up type safety at that call site.

Features

  • Long-Lived, Stateful Compute: Each unit of compute is like a tiny server that remembers things between requests – no need to re-fetch data from a database or worry about timeouts. Like AWS Lambda, but with memory and no timeouts.
  • Blazing-Fast Reads & Writes: State is stored on the same machine as your compute, so reads and writes are ultra-fast. No database round trips, no latency spikes. State is persisted to Rivet for long term storage, so it survives server restarts.
  • Realtime: Update state and broadcast changes in realtime with WebSockets. No external pub/sub systems, no polling – just built-in low-latency events.
  • Infinitely Scalable: Automatically scale from zero to millions of concurrent actors. Pay only for what you use with instant scaling and no cold starts.
  • Fault Tolerant: Built-in error handling and recovery. Actors automatically restart on failure while preserving state integrity and continuing operations.

When to Use Rivet Actors

  • AI agents & sandboxes: multi-step toolchains, conversation memory, sandbox orchestration.
  • Multiplayer or collaborative apps: CRDT docs, shared cursors, realtime dashboards, chat.
  • Workflow automation: background jobs, cron, rate limiters, durable queues, backpressure control.
  • Data-intensive backends: geo-distributed or per-tenant databases, in-memory caches, sharded SQL.
  • Networking workloads: WebSocket servers, custom protocols, local-first sync, edge fanout.

Minimal Project

Backend

actors.ts

import { actor, event, setup } from "rivetkit";

const counter = actor({
  state: { count: 0 },
  events: {
    count: event<number>(),
  },
  actions: {
    increment: (c, amount: number) => {
      c.state.count += amount;
      c.broadcast("count", c.state.count);
      return c.state.count;
    },
  },
});

export const registry = setup({
  use: { counter },
});

server.ts

Integrate with the user's existing server if applicable. Otherwise, default to Hono.

No Framework

import { registry } from "./actors";

export default registry.serve();

Hono

import { Hono } from "hono";
import { registry } from "./actors";

const app = new Hono();
app.all("/api/rivet/*", (c) => registry.handler(c.req.raw));

export default app;

Elysia

import { Elysia } from "elysia";
import { registry } from "./actors";

const app = new Elysia()
	.all("/api/rivet/*", (c) => registry.handler(c.request));

export default app;

Client Docs

Use the client SDK that matches your app:

Actor Quick Reference

In-Memory State

Persistent data that survives restarts, crashes, and deployments. State is persisted on Rivet Cloud or Rivet self-hosted, so it survives restarts if the current process crashes or exits.

Static Initial State

import { actor } from "rivetkit";

const counter = actor({
  state: { count: 0 },
  actions: {
    increment: (c) => c.state.count += 1,
  },
});

Dynamic Initial State

import { actor } from "rivetkit";

interface CounterState {
  count: number;
}

const counter = actor({
  state: { count: 0 } as CounterState,
  createState: (c, input: { start?: number }): CounterState => ({
    count: input.start ?? 0,
  }),
  actions: {
    increment: (c) => c.state.count += 1,
  },
});

Documentation

Keys

Keys uniquely identify actor instances. Use compound keys (arrays) for hierarchical addressing:

import { actor, setup } from "rivetkit";
import { createClient } from "rivetkit/client";

const chatRoom = actor({
  state: { messages: [] as string[] },
  actions: {
    getRoomInfo: (c) => ({ org: c.key[0], room: c.key[1] }),
  },
});

const registry = setup({ use: { chatRoom } });
const client = createClient<typeof registry>();

// Compound key: [org, room]
client.chatRoom.getOrCreate(["org-acme", "general"]);

// Access key inside actor via c.key

Don't build keys with string interpolation like "org:${userId}" when userId contains user data. Use arrays instead to prevent key injection attacks.

Documentation

Input

Pass initialization data when creating actors.

import { actor, setup } from "rivetkit";
import { createClient } from "rivetkit/client";

const game = actor({
  createState: (c, input: { mode: string }) => ({ mode: input.mode }),
  actions: {},
});

const registry = setup({ use: { game } });
const client = createClient<typeof registry>();

// Client usage
const gameHandle = client.game.getOrCreate(["game-1"], {
  createWithInput: { mode: "ranked" }
});

Documentation

Temporary Variables

Temporary data that doesn't survive restarts. Use for non-serializable objects (event emitters, connections, etc).

Static Initial Vars

import { actor } from "rivetkit";

const counter = actor({
  state: { count: 0 },
  vars: { lastAccess: 0 },
  actions: {
    increment: (c) => {
      c.vars.lastAccess = Date.now();
      return c.state.count += 1;
    },
  },
});

Dynamic Initial Vars

import { actor } from "rivetkit";

const counter = actor({
  state: { count: 0 },
  createVars: () => ({
    emitter: new EventTarget(),
  }),
  actions: {
    increment: (c) => {
      c.vars.emitter.dispatchEvent(new Event("change"));
      return c.state.count += 1;
    },
  },
});

Documentation

Actions

Actions are the primary way clients and other actors communicate with an actor.

import { actor } from "rivetkit";

const counter = actor({
  state: { count: 0 },
  actions: {
    increment: (c, amount: number) => (c.state.count += amount),
    getCount: (c) => c.state.count,
  },
});

Documentation

Events & Broadcasts

Events enable real-time communication from actors to connected clients.

import { actor, event } from "rivetkit";

const chatRoom = actor({
  state: { messages: [] as string[] },
  events: {
    newMessage: event<{ text: string }>(),
  },
  actions: {
    sendMessage: (c, text: string) => {
      // Broadcast to ALL connected clients
      c.broadcast("newMessage", { text });
    },
  },
});

Documentation

Connections

Access the current connection via c.conn or all connected clients via c.conns. Use c.conn.id or c.conn.state to securely identify who is calling an action. Connection state is initialized via connState or createConnState, which receives parameters passed by the client on connect.

Static Connection Initial State

import { actor } from "rivetkit";

const chatRoom = actor({
  state: {},
  connState: { visitorId: 0 },
  onConnect: (c, conn) => {
    conn.state.visitorId = Math.random();
  },
  actions: {
    whoAmI: (c) => c.conn.state.visitorId,
  },
});

Dynamic Connection Initial State

import { actor } from "rivetkit";

const chatRoom = actor({
  state: {},
  // params passed from client
  createConnState: (c, params: { userId: string }) => ({
    userId: params.userId,
  }),
  actions: {
    // Access current connection's state and params
    whoAmI: (c) => ({
      state: c.conn.state,
      params: c.conn.params,
    }),
    // Iterate all connections with c.conns
    notifyOthers: (c, text: string) => {
      for (const conn of c.conns.values()) {
        if (conn !== c.conn) conn.send("notification", { text });
      }
    },
  },
});

Documentation

Queues

Use queues to process durable messages in order inside a run loop.

imp

...

用户评价 (0)

发表评价

效果
易用性
文档
兼容性

暂无评价

统计数据

安装量6.0K
评分4.4 / 5.0
版本
更新日期2026年5月22日
对比案例1 组

用户评分

4.4(39)
5
49%
4
51%
3
0%
2
0%
1
0%

为此 Skill 评分

0.0

兼容平台

🔧Claude Code
🔧OpenClaw
🔧OpenCode
🔧Codex
🔧Gemini CLI
🔧GitHub Copilot
🔧Amp
🔧Kimi CLI

时间线

创建2026年3月16日
最后更新2026年5月22日