|
| 1 | +# Session limits |
| 2 | + |
| 3 | +Session limits let an application set an AI Credits budget for a Copilot session. Use `sessionLimits` when creating or resuming a session to cap the current accounting window. |
| 4 | + |
| 5 | +## Configure a session limit |
| 6 | + |
| 7 | +Set `maxAiCredits` to the maximum AI Credits the session can consume in its current accounting window. The SDK forwards this value to the Copilot CLI when it creates or resumes the session. |
| 8 | + |
| 9 | +<details open> |
| 10 | +<summary><strong>TypeScript</strong></summary> |
| 11 | + |
| 12 | +<!-- docs-validate: skip --> |
| 13 | + |
| 14 | +```typescript |
| 15 | +const session = await client.createSession({ |
| 16 | + onPermissionRequest: approveAll, |
| 17 | + sessionLimits: { |
| 18 | + maxAiCredits: 30, |
| 19 | + }, |
| 20 | +}); |
| 21 | + |
| 22 | +const resumed = await client.resumeSession(session.sessionId, { |
| 23 | + onPermissionRequest: approveAll, |
| 24 | + sessionLimits: { |
| 25 | + maxAiCredits: 30, |
| 26 | + }, |
| 27 | +}); |
| 28 | +``` |
| 29 | + |
| 30 | +</details> |
| 31 | +<details> |
| 32 | +<summary><strong>Python</strong></summary> |
| 33 | + |
| 34 | +<!-- docs-validate: skip --> |
| 35 | + |
| 36 | +```python |
| 37 | +session = await client.create_session( |
| 38 | + on_permission_request=PermissionHandler.approve_all, |
| 39 | + session_limits={ |
| 40 | + "max_ai_credits": 30, |
| 41 | + }, |
| 42 | +) |
| 43 | + |
| 44 | +resumed = await client.resume_session( |
| 45 | + session.session_id, |
| 46 | + on_permission_request=PermissionHandler.approve_all, |
| 47 | + session_limits={ |
| 48 | + "max_ai_credits": 30, |
| 49 | + }, |
| 50 | +) |
| 51 | +``` |
| 52 | + |
| 53 | +</details> |
| 54 | +<details> |
| 55 | +<summary><strong>Go</strong></summary> |
| 56 | + |
| 57 | +<!-- docs-validate: skip --> |
| 58 | + |
| 59 | +```go |
| 60 | +session, err := client.CreateSession(ctx, &copilot.SessionConfig{ |
| 61 | + OnPermissionRequest: copilot.PermissionHandler.ApproveAll, |
| 62 | + SessionLimits: &rpc.SessionLimitsConfig{ |
| 63 | + MaxAiCredits: copilot.Float64(30), |
| 64 | + }, |
| 65 | +}) |
| 66 | + |
| 67 | +resumed, err := client.ResumeSession(ctx, session.SessionID, &copilot.ResumeSessionConfig{ |
| 68 | + OnPermissionRequest: copilot.PermissionHandler.ApproveAll, |
| 69 | + SessionLimits: &rpc.SessionLimitsConfig{ |
| 70 | + MaxAiCredits: copilot.Float64(30), |
| 71 | + }, |
| 72 | +}) |
| 73 | +``` |
| 74 | + |
| 75 | +</details> |
| 76 | +<details> |
| 77 | +<summary><strong>.NET</strong></summary> |
| 78 | + |
| 79 | +<!-- docs-validate: skip --> |
| 80 | + |
| 81 | +```csharp |
| 82 | +var session = await client.CreateSessionAsync(new SessionConfig |
| 83 | +{ |
| 84 | + OnPermissionRequest = PermissionHandler.ApproveAll, |
| 85 | + SessionLimits = new SessionLimitsConfig |
| 86 | + { |
| 87 | + MaxAiCredits = 30, |
| 88 | + }, |
| 89 | +}); |
| 90 | + |
| 91 | +var resumed = await client.ResumeSessionAsync(session.SessionId, new ResumeSessionConfig |
| 92 | +{ |
| 93 | + OnPermissionRequest = PermissionHandler.ApproveAll, |
| 94 | + SessionLimits = new SessionLimitsConfig |
| 95 | + { |
| 96 | + MaxAiCredits = 30, |
| 97 | + }, |
| 98 | +}); |
| 99 | +``` |
| 100 | + |
| 101 | +</details> |
| 102 | +<details> |
| 103 | +<summary><strong>Java</strong></summary> |
| 104 | + |
| 105 | +<!-- docs-validate: skip --> |
| 106 | + |
| 107 | +```java |
| 108 | +CopilotSession session = client |
| 109 | + .createSession(new SessionConfig() |
| 110 | + .setOnPermissionRequest(PermissionHandler.APPROVE_ALL) |
| 111 | + .setSessionLimits(new SessionLimitsConfig(30.0))) |
| 112 | + .get(); |
| 113 | + |
| 114 | +CopilotSession resumed = client |
| 115 | + .resumeSession(session.getSessionId(), new ResumeSessionConfig() |
| 116 | + .setOnPermissionRequest(PermissionHandler.APPROVE_ALL) |
| 117 | + .setSessionLimits(new SessionLimitsConfig(30.0))) |
| 118 | + .get(); |
| 119 | +``` |
| 120 | + |
| 121 | +</details> |
| 122 | +<details> |
| 123 | +<summary><strong>Rust</strong></summary> |
| 124 | + |
| 125 | +<!-- docs-validate: skip --> |
| 126 | + |
| 127 | +```rust |
| 128 | +let limits = SessionLimitsConfig { |
| 129 | + max_ai_credits: Some(30.0), |
| 130 | +}; |
| 131 | + |
| 132 | +let session = client |
| 133 | + .create_session( |
| 134 | + SessionConfig::new() |
| 135 | + .approve_all_permissions() |
| 136 | + .with_session_limits(limits.clone()), |
| 137 | + ) |
| 138 | + .await?; |
| 139 | + |
| 140 | +let resumed = client |
| 141 | + .resume_session( |
| 142 | + ResumeSessionConfig::new(session.id().clone()) |
| 143 | + .approve_all_permissions() |
| 144 | + .with_session_limits(limits), |
| 145 | + ) |
| 146 | + .await?; |
| 147 | +``` |
| 148 | + |
| 149 | +</details> |
| 150 | + |
| 151 | +## Observe budget events |
| 152 | + |
| 153 | +Applications can subscribe to session events to update UI when the limit changes or the session exhausts its budget. |
| 154 | + |
| 155 | +| Event type | When it is emitted | Important fields | |
| 156 | +|---|---|---| |
| 157 | +| `session.session_limits_changed` | Active session limits changed. A `null` `sessionLimits` value means no limits are active. | `sessionLimits.maxAiCredits?` | |
| 158 | +| `session.usage_checkpoint` | The runtime records durable aggregate usage for resume and accounting. | `totalNanoAiu`, `totalPremiumRequests?` | |
| 159 | +| `session_limits_exhausted.requested` | The session exhausted its configured budget and needs a user decision before continuing. | `requestId`, `maxAiCredits`, `usedAiCredits` | |
| 160 | +| `session_limits_exhausted.completed` | The exhausted-limit prompt was resolved. | `requestId`, `response.action`, `response.additionalAiCredits?`, `response.maxAiCredits?` | |
| 161 | + |
| 162 | +Use the generated event types for the SDK language you are using. For example, TypeScript narrows by `event.type`: |
| 163 | + |
| 164 | +```typescript |
| 165 | +session.on((event) => { |
| 166 | + if (event.type === "session_limits_exhausted.requested") { |
| 167 | + showBudgetDialog({ |
| 168 | + requestId: event.data.requestId, |
| 169 | + maxAiCredits: event.data.maxAiCredits, |
| 170 | + usedAiCredits: event.data.usedAiCredits, |
| 171 | + }); |
| 172 | + } |
| 173 | +}); |
| 174 | +``` |
| 175 | + |
| 176 | +## Current limitations |
| 177 | + |
| 178 | +The SDKs expose session limits when creating or resuming a session. They do not currently expose a high-level convenience API for changing or clearing limits on an already-running session. To apply a different limit through the public SDK surface, create or resume a session with a new `sessionLimits` value. |
0 commit comments