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.
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:
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
Install @camelai/run
npm install @camelai/run@camelai/run v2 has zero runtime dependencies and uses standard fetch and WebSocket.
Create and run an agent
Save this as 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:
npx tsx quickstart.mtsStream 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:
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:
curl -s "$CAMELRUN_BASE_URL/v2/models?available=true" \
-H "Authorization: Bearer $CAMELRUN_API_KEY" | jq -r '.[].id'Pass any model ID to upsert:
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:
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:
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:
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 anAgentHandlecarrying itsid(agt_...),key, and scopedagentToken(agt_tok_...).- Because
weatherhas anexecutefunction, 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 withwait: trueand returned aRunRecordcontainingstatus,text,output,inputs,toolCalls,filesChanged,usage, anderror. Ifstatus === "failed",agent.run()throws aCamelRunErrorunless you pass{ throwOnError: false }.client.close()closed the local WebSocket cleanly while the agent remained stored in camelRun.
Next steps
Learn about keyed agents, queue modes, steering, compaction, forking, and dreaming.
Attach Git-backed repositories, read and write files, and run bash in session VMs.
Compare local WebSocket tools, HTTP webhook tools, signed MCP servers, and built-ins.
Stream live replies and handle human inputs in a web application.