camelAI Documentation

Start Here

camelRun v2 quickstart: run your first AI agent

Install @camelai/run, upsert a durable keyed agent with a local tool, stream a run, and call the /v2 REST API.

View as Markdown

This guide gets a durable camelRun v2 agent running in a few minutes. Your tool runs inside your own process over a hibernatable WebSocket while camelRun executes the model loop, stores history, and manages retries.

Prerequisites

  • Node 22 or later, Bun, Deno, or Cloudflare Workers
  • A camelRun v2 tenant API key (cr_live_<tenantId>.<id>_<secret>)

Export your API key and base URL:

bash
export CAMELRUN_BASE_URL="https://run.camelai.com"
export CAMELRUN_API_KEY="cr_live_..."

Keep tenant API keys on your server. Never embed them in browser code.

Run your first agent with the TypeScript SDK

1

Install @camelai/run

bash
npm install @camelai/run

@camelai/run v2 has zero runtime dependencies and uses standard fetch and WebSocket.

2

Create and run an agent

Save this as quickstart.mts:

quickstart.mts
import { CamelRun, defineTool } from "@camelai/run";

const client = new CamelRun({
  baseUrl: process.env.CAMELRUN_BASE_URL ?? "https://run.camelai.com",
  apiKey: process.env.CAMELRUN_API_KEY!,
});

const weather = defineTool({
  name: "weather",
  description: "Get today's weather for a city",
  parameters: {
    type: "object",
    properties: {
      city: { type: "string", description: "City name" },
    },
    required: ["city"],
  },
  execute: ({ city }: { city: string }) => ({
    city,
    forecast: "sunny",
    highC: 24,
  }),
});

// Upsert by key: running this script again reuses the same agent and history.
const agent = await client.agents.upsert("quickstart", {
  instructions: "You are a concise assistant.",
  tools: [weather],
});

const run = await agent.run("Should I bring an umbrella in Lisbon today?");
console.log(run.text);

client.close();

Run it:

bash
npx tsx quickstart.mts
3

Stream events live

Use agent.stream() to receive text_delta, tool_call_start, tool_call_end, and terminal events over SSE, then await stream.result for the final RunRecord:

ts
const stream = agent.stream("What about tomorrow?");
for await (const event of stream) {
  if (event.type === "text_delta") {
    const data = event.data as { delta?: string };
    process.stdout.write(data.delta ?? "");
  }
  if (event.type === "tool_call_start") {
    const data = event.data as { name?: string };
    console.log(`\n[calling ${data.name}]`);
  }
}
const finalRun = await stream.result;
console.log(`\nStatus: ${finalRun.status} | Cost: $${finalRun.usage.costUsd}`);

Choose a model

When you omit model, camelRun uses the runtime default (azure/gpt-6.2-sol, also aliased as camelrun/default). You can inspect every model available to your tenant:

bash
curl -s "$CAMELRUN_BASE_URL/v2/models?available=true" \
  -H "Authorization: Bearer $CAMELRUN_API_KEY" | jq -r '.[].id'

Pass any model ID to upsert:

ts
const agent = await client.agents.upsert("quickstart", {
  model: "openrouter/meta/muse-ember",
  instructions: "You are a concise assistant.",
  tools: [weather],
});

To use your own OpenAI, Anthropic, Gemini, OpenRouter, Azure, or custom provider key, store it once with PUT /v2/providers/:provider. See Models and keys.

Run with curl

Every HTTP endpoint works directly with curl or any language's HTTP client. You can even create an agent and start its first run in a single PUT /v2/agents/:key call by including prompt:

bash
BASE="${CAMELRUN_BASE_URL:-https://run.camelai.com}"
AUTH="Authorization: Bearer $CAMELRUN_API_KEY"

# Create (or update) a keyed agent and run a first prompt synchronously (wait defaults to true)
curl -s -X PUT "$BASE/v2/agents/quickstart-curl" \
  -H "$AUTH" \
  -H "Content-Type: application/json" \
  -d '{
    "instructions": "You are a concise assistant.",
    "prompt": "Write a haiku about durable agents.",
    "wait": true
  }' | jq '{id, key, model, text: .run.text, status: .run.status}'

Send a follow-up run on the same agent:

bash
curl -s -X POST "$BASE/v2/agents/quickstart-curl/runs" \
  -H "$AUTH" \
  -H "Content-Type: application/json" \
  -d '{
    "prompt": "Now write one about Git-backed workspaces.",
    "idempotencyKey": "haiku-2",
    "wait": true
  }' | jq '{id, status, text, usage}'

Stream events for an async run (wait: false) using SSE:

bash
RUN_ID=$(curl -s -X POST "$BASE/v2/agents/quickstart-curl/runs" \
  -H "$AUTH" \
  -H "Content-Type: application/json" \
  -d '{"prompt": "Count to five slowly.", "wait": false}' | jq -r .id)

curl -N "$BASE/v2/agents/quickstart-curl/events?after=0&runId=$RUN_ID" \
  -H "$AUTH" \
  -H "Accept: text/event-stream"

What just happened

  • client.agents.upsert("quickstart", ...) created a durable agent keyed by "quickstart" (or updated it if it already existed) and returned an AgentHandle carrying its id (agt_...), key, and scoped agentToken (agt_tok_...).
  • Because weather has an execute function, the SDK automatically opened a WebSocket to /v2/agents/:idOrKey/ws, registered ["weather"], executed the tool when the model called it, and returned the result.
  • agent.run() submitted the prompt with wait: true and returned a RunRecord containing status, text, output, inputs, toolCalls, filesChanged, usage, and error. If status === "failed", agent.run() throws a CamelRunError unless you pass { throwOnError: false }.
  • client.close() closed the local WebSocket cleanly while the agent remained stored in camelRun.

Next steps