Integrate
Agent frameworks
Status#
Principles#
- The model never sees the API key. It lives in your runtime and is scoped to what the agent needs (the Agent runtime preset).
- The agent id is set by your code, not by the model, so a prompt cannot make one agent spend another's budget.
- Refusals and pending approvals are results the model reads, not exceptions that make it retry.
- Approvals stay with people. No tool approves payments: the API refuses approval calls from API keys.
- Each decision to buy gets one Idempotency-Key, stored with the task, so retries never pay twice.
One tool, any framework#
Every adapter would expose the same small tool set over the REST API: find services, pay for one, and check the remaining budget. The tool definition, in JSON Schema:
{
"name": "pay_for_service",
"description": "Buy one answer from a paid CortexPay service. The owner's spending policy decides; a refusal is a normal result.",
"input_schema": {
"type": "object",
"properties": {
"serviceId": { "type": "string", "description": "Service id from find_services" },
"input": { "type": "object", "description": "JSON sent to the service" }
},
"required": ["serviceId", "input"]
}
}import { CortexPay, CortexPayError } from "@cortexpay/sdk";
const cortex = new CortexPay({ apiKey: process.env.CORTEXPAY_API_KEY!, baseUrl: "https://cortexpay.vercel.app" });
const AGENT_ID = process.env.CORTEXPAY_AGENT_ID!; // fixed by your code, never chosen by the model
/** The handler every framework adapter would call. Returns text the model can read. */
export async function payForService(args: { serviceId: string; input: Record<string, unknown> }, taskKey: string) {
try {
const r = await cortex.services.request(
{ agentId: AGENT_ID, serviceId: args.serviceId, input: args.input },
{ idempotencyKey: taskKey }, // one key per decision, reused on retries
);
if (r.status === "delivered") return JSON.stringify(r.delivery?.data);
if (r.status === "awaiting_approval") return `Waiting for the owner to approve (approval ${r.approvalId}). Do not retry.`;
return `Not paid: ${r.evaluation?.reason ?? r.error?.message ?? r.status}`;
} catch (e) {
if (e instanceof CortexPayError) return `CortexPay error ${e.code}: ${e.message}`;
throw e;
}
}find_services maps to GET /api/services and check_budget to GET /api/agents/:id/account.
MCP#
A Model Context Protocol server would register the three tools above and run them with a key held by the server process. Hosts that speak MCP (desktop assistants, IDEs, agent runtimes) would then see CortexPay as tools without custom code. One server per agent keeps the agent id fixed.
Tool calling#
With a model provider's tool or function calling, pass the JSON Schema above as a tool. When the model calls it, run the handler and return its text as the tool result. The model can then explain a refusal or tell the user an approval is pending.
LangGraph#
- A payment node calls the handler with a key stored in the graph state for this task.
- On
awaiting_approval, the node interrupts the graph and stores theapprovalId. - Your webhook endpoint receives
approval.decided(see Webhooks) and resumes the graph. FetchingGET /api/services/requests/:idthen gives the final result.
ElizaOS#
An ElizaOS plugin would add a PAY_FOR_SERVICE action that validates the requested service against the catalogue, calls the handler and posts the result to the conversation. The key and agent id would come from the character's secrets, never from a message.
Anything else
Any runtime that can make an HTTPS request can use CortexPay. Start from Getting started.