Skip to main content

Session limits

Session limits let an application set an AI Credits budget for a Copilot session. Use sessionLimits when creating or resuming a session to set a soft cap for the current accounting window.

在本文中

Configure a session limit

Set maxAiCredits to the AI Credits soft cap for the session's current accounting window. Usage is checked after model calls return, so one response can exceed the configured value before the runtime blocks the next model call. The SDK forwards this value to the Copilot CLI when it creates or resumes the session.

代码语言 navigation

TypeScript
const session = await client.createSession({
    onPermissionRequest: approveAll,
    sessionLimits: {
        maxAiCredits: 30,
    },
});

const resumed = await client.resumeSession(session.sessionId, {
    onPermissionRequest: approveAll,
    sessionLimits: {
        maxAiCredits: 30,
    },
});

Observe budget events

Applications can subscribe to session events to update UI when the soft cap changes or the session reaches the exhausted-budget flow.

Event typeWhen it is emittedImportant fields
session.session_limits_changedActive session limits changed. A null sessionLimits value means no limits are active.sessionLimits.maxAiCredits?
session.usage_checkpointThe runtime records durable aggregate usage for resume and accounting.totalNanoAiu, totalPremiumRequests?
session_limits_exhausted.requestedThe session reached the exhausted-budget flow and needs a user decision before continuing.requestId, maxAiCredits, usedAiCredits
session_limits_exhausted.completedThe exhausted-limit prompt was resolved.requestId, response.action, response.additionalAiCredits?, response.maxAiCredits?

Use the generated event types for the SDK language you are using. For example, TypeScript narrows by event.type:

session.on((event) => {
    if (event.type === "session_limits_exhausted.requested") {
        showBudgetDialog({
            requestId: event.data.requestId,
            maxAiCredits: event.data.maxAiCredits,
            usedAiCredits: event.data.usedAiCredits,
        });
    }
});