Isolate customer spaces with OpenAI Agents SDK
Provision a separate space for each customer. Your backend holds the organization management key and gives each agent a client scoped to that customer’s space. The tools close over the client selected by the backend.
Use Node.js 24 or newer, an OpenAI API key, and a Nicia management key minted by an operator from your organization.
mkdir openai-agents-sdkcd openai-agents-sdknpm init -ynpm pkg set type=modulenpm install @nicia-ai/client@^0.1.1 @openai/agents@0.22.0 zod@4.6.7Save this as recipe.ts.
import { randomUUID } from "node:crypto";import { Agent, run, tool, type Model } from "@openai/agents";import { z } from "zod";import { NiciaOrgClient, NiciaSpaceClient } from "@nicia-ai/client";
export async function provisionCustomer( baseUrl: string, managementKey: string, customerId: string,) { if (!/^[a-z0-9-]{1,40}$/.test(customerId)) throw new Error("Use a lowercase customer ID"); const handle = `tenant:${customerId}`; const org = new NiciaOrgClient({ baseUrl, managementKey }); await org.putSpace(handle, { name: `Support for ${customerId}`, subject: { kind: "tenant", externalId: customerId, label: customerId }, }); const { secret } = await org.mintSpaceKey(handle, { name: "Customer support demo", capabilities: ["read", "write"], expiresAt: new Date(Date.now() + 3_600_000).toISOString(), }); if (!secret) throw new Error("Space key mint returned no secret"); return new NiciaSpaceClient({ baseUrl, handle, credential: secret });}
export function customerAgent( space: NiciaSpaceClient, model: string | Model = "gpt-4.1-mini",) { return new Agent({ name: "Customer support", model, instructions: "Use context to answer from this customer's knowledge. Use save only when the user explicitly asks to save a note. Report the returned admission status.", tools: [ tool({ name: "context", description: "Retrieve receipted context for this customer.", parameters: z.object({ prompt: z.string().min(1) }), execute: ({ prompt }) => space.getContext({ prompt }), }), tool({ name: "save", description: "Save a support note in this customer's space.", parameters: z.object({ title: z.string().min(1).max(120), body: z.string().min(1).max(4_000), }), execute: async ({ title, body }) => { const id = `note-${randomUUID()}`; const { change } = await space.writeRecords({ records: [{ id, kind: "note", label: title, fields: { body } }], }); return { id, status: change.status, changeId: change.id }; }, }), ], });}
if (import.meta.main) { const { NICIA_MANAGEMENT_KEY, NICIA_API_URL = "https://api.nicia.ai" } = process.env; const [customerId, prompt] = process.argv.slice(2); if (!NICIA_MANAGEMENT_KEY || !customerId || !prompt) { throw new Error( 'Set NICIA_MANAGEMENT_KEY and run node recipe.ts <customer-id> "<prompt>"', ); } const space = await provisionCustomer( NICIA_API_URL, NICIA_MANAGEMENT_KEY, customerId, ); const result = await run(customerAgent(space), prompt); console.log(result.finalOutput);}Run the customer agent
Section titled “Run the customer agent”read -rsp 'Nicia management key: ' NICIA_MANAGEMENT_KEY; printf '\n'; export NICIA_MANAGEMENT_KEYread -rsp 'OpenAI API key: ' OPENAI_API_KEY; printf '\n'; export OPENAI_API_KEYnode recipe.ts acme 'Save a support note titled Kickoff saying our kickoff is Tuesday.'node recipe.ts acme 'Use the saved kickoff note to tell me when kickoff is.'node recipe.ts globex 'Use our saved notes to tell me when kickoff is.'The first run reports the write’s admission status. If it was applied, Acme’s next run can retrieve it. The Globex run queries its own space. Use different scratch customer IDs if those names already exist in your organization.
For an application, resolve the customer ID from your authenticated account mapping before binding the tools. Mint the scoped key during onboarding and store the one-time secret in your backend’s secret store. This demo mints a short-lived key on each run so it can run from a single command.