Users and spend
Multi-user apps on camelRun v2: identity, actors, key scopes, and spend caps
Isolate users in a multi-tenant app on camelRun v2 using keyed agents, toolIdentity, run actors, per-user keyScopes, and spendLimitUsd.
When your application serves many end users on one camelRun v2 tenant, combine five primitives to keep each user's conversations, tools, provider keys, and spending isolated.
1. Key agents per user or conversation
Give each user or thread its own agent key and, when file tools or bash are enabled, its own CamelFS repository:
const repo = await client.repos.create({ name: `user-${user.id}-workspace` });
const agent = await client.agents.upsert(`user-${user.id}-assistant`, {
definition: "app-assistant",
repoId: repo.repoId,
toolIdentity: {
subject: user.id,
context: { orgId: user.orgId, role: user.role },
},
spendLimitUsd: 10,
});2. Pass actor on every run and input response
When multiple collaborators share one agent (for example a shared project builder or team channel), pass actor on POST /v2/agents/:idOrKey/runs using your tenant API key:
const run = await agent.run(prompt, {
actor: currentUser.id,
whileRunning: "queue",
spendLimitUsd: remainingUserBudgetUsd,
});What actor enforces:
- Signed MCP identity: Signed runtime JWTs (
auth: { type: "runtime" }) include bothsub(the agent'stoolIdentity.subject) andact(the run'sactor). Your MCP server knows both which agent owner is configured and which authenticated app user triggered this run. - Steer isolation: A
whileRunning: "steer"request from a differentactorcannot hijack an active run; camelRun automatically queues it as its own run. - Batch isolation: Runs with an
actoralways execute individually, never merged into another user's batch. - Input ownership: Only a tenant credential supplying the matching
actorcan answer pending inputs created by that run (POST /v2/agents/:idOrKey/inputs/:inputId). Any other caller receives403 input_actor_mismatch. - Tenant-only: Passing
actorwith an agent-scoped token (agt_tok_...) is rejected with403 forbidden.
3. Authenticate tool calls with toolIdentity and runtime JWTs
Never pass user IDs or organization IDs as tool parameters that the model fills in. Instead, configure toolIdentity: { subject, context } when upserting the agent and set auth: { type: "runtime" } on your MCP server. Your server verifies the Ed25519 JWT against GET /v2/.well-known/jwks.json and reads sub, act, ctx, tenant, and agent from verified claims. See Tools.
4. Per-user BYOK with key scopes
If your customers bring their own OpenAI, Anthropic, Gemini, or OpenRouter keys, store each customer's keys under their own keyScope:
curl -s -X PUT "https://run.camelai.com/v2/key-scopes/org_42/providers/anthropic" \
-H "Authorization: Bearer $CAMELRUN_API_KEY" \
-H "Content-Type: application/json" \
-d '{"apiKey": "sk-ant-..."}'Then set keyScope: "org_42" when upserting the agent (PUT /v2/agents/:key) or on individual runs (POST /v2/agents/:idOrKey/runs). Scoped agents never fall back to your platform keys.
5. Enforce customer spend limits in your application
camelRun v2 does not maintain per-end-user wallets inside the runtime (/v1/end-users/* was removed). Instead:
- Check your user's remaining allowance or credit balance in your own database before starting a run.
- Pass that remaining budget as
spendLimitUsdonPOST /v2/agents/:idOrKey/runs(must usewhileRunning: "queue"). - When the run finishes (or via
run.completed/usage.recordedwebhooks), deductrun.usage.costUsdfrom the user's balance in your database. - Use
PATCH /v2/agents/:idOrKey/spend-limitto adjust an agent's overall cap safely while idle.