-
Notifications
You must be signed in to change notification settings - Fork 1.3k
docs: add session limits guidance #1856
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+219
−0
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,174 @@ | ||
| # 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. | ||
|
|
||
| <details open> | ||
| <summary><strong>TypeScript</strong></summary> | ||
|
|
||
| <!-- docs-validate: skip --> | ||
|
|
||
| ```typescript | ||
| const session = await client.createSession({ | ||
| onPermissionRequest: approveAll, | ||
| sessionLimits: { | ||
| maxAiCredits: 30, | ||
| }, | ||
| }); | ||
|
|
||
| const resumed = await client.resumeSession(session.sessionId, { | ||
| onPermissionRequest: approveAll, | ||
| sessionLimits: { | ||
| maxAiCredits: 30, | ||
| }, | ||
| }); | ||
| ``` | ||
|
|
||
| </details> | ||
| <details> | ||
| <summary><strong>Python</strong></summary> | ||
|
|
||
| <!-- docs-validate: skip --> | ||
|
|
||
| ```python | ||
| session = await client.create_session( | ||
| on_permission_request=PermissionHandler.approve_all, | ||
| session_limits={ | ||
| "max_ai_credits": 30, | ||
| }, | ||
| ) | ||
|
|
||
| resumed = await client.resume_session( | ||
| session.session_id, | ||
| on_permission_request=PermissionHandler.approve_all, | ||
| session_limits={ | ||
| "max_ai_credits": 30, | ||
| }, | ||
| ) | ||
| ``` | ||
|
|
||
| </details> | ||
| <details> | ||
| <summary><strong>Go</strong></summary> | ||
|
|
||
| <!-- docs-validate: skip --> | ||
|
|
||
| ```go | ||
| session, err := client.CreateSession(ctx, &copilot.SessionConfig{ | ||
| OnPermissionRequest: copilot.PermissionHandler.ApproveAll, | ||
| SessionLimits: &rpc.SessionLimitsConfig{ | ||
| MaxAiCredits: copilot.Float64(30), | ||
| }, | ||
| }) | ||
|
|
||
| resumed, err := client.ResumeSession(ctx, session.SessionID, &copilot.ResumeSessionConfig{ | ||
| OnPermissionRequest: copilot.PermissionHandler.ApproveAll, | ||
| SessionLimits: &rpc.SessionLimitsConfig{ | ||
| MaxAiCredits: copilot.Float64(30), | ||
| }, | ||
| }) | ||
| ``` | ||
|
|
||
| </details> | ||
| <details> | ||
| <summary><strong>.NET</strong></summary> | ||
|
|
||
| <!-- docs-validate: skip --> | ||
|
|
||
| ```csharp | ||
| var session = await client.CreateSessionAsync(new SessionConfig | ||
| { | ||
| OnPermissionRequest = PermissionHandler.ApproveAll, | ||
| SessionLimits = new SessionLimitsConfig | ||
| { | ||
| MaxAiCredits = 30, | ||
| }, | ||
| }); | ||
|
|
||
| var resumed = await client.ResumeSessionAsync(session.SessionId, new ResumeSessionConfig | ||
| { | ||
| OnPermissionRequest = PermissionHandler.ApproveAll, | ||
| SessionLimits = new SessionLimitsConfig | ||
| { | ||
| MaxAiCredits = 30, | ||
| }, | ||
| }); | ||
| ``` | ||
|
|
||
| </details> | ||
| <details> | ||
| <summary><strong>Java</strong></summary> | ||
|
|
||
| <!-- docs-validate: skip --> | ||
|
|
||
| ```java | ||
| CopilotSession session = client | ||
| .createSession(new SessionConfig() | ||
| .setOnPermissionRequest(PermissionHandler.APPROVE_ALL) | ||
| .setSessionLimits(new SessionLimitsConfig(30.0))) | ||
| .get(); | ||
|
|
||
| CopilotSession resumed = client | ||
| .resumeSession(session.getSessionId(), new ResumeSessionConfig() | ||
| .setOnPermissionRequest(PermissionHandler.APPROVE_ALL) | ||
| .setSessionLimits(new SessionLimitsConfig(30.0))) | ||
| .get(); | ||
| ``` | ||
|
|
||
| </details> | ||
| <details> | ||
| <summary><strong>Rust</strong></summary> | ||
|
|
||
| <!-- docs-validate: skip --> | ||
|
|
||
| ```rust | ||
| let limits = SessionLimitsConfig { | ||
| max_ai_credits: Some(30.0), | ||
| }; | ||
|
|
||
| let session = client | ||
| .create_session( | ||
| SessionConfig::new() | ||
| .approve_all_permissions() | ||
| .with_session_limits(limits.clone()), | ||
| ) | ||
| .await?; | ||
|
|
||
| let resumed = client | ||
| .resume_session( | ||
| ResumeSessionConfig::new(session.id().clone()) | ||
| .approve_all_permissions() | ||
| .with_session_limits(limits), | ||
| ) | ||
| .await?; | ||
| ``` | ||
|
|
||
| </details> | ||
|
|
||
| ## 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 type | When it is emitted | Important fields | | ||
| |---|---|---| | ||
| | `session.session_limits_changed` | Active session limits changed. A `null` `sessionLimits` value means no limits are active. | `sessionLimits.maxAiCredits?` | | ||
| | `session.usage_checkpoint` | The runtime records durable aggregate usage for resume and accounting. | `totalNanoAiu`, `totalPremiumRequests?` | | ||
| | `session_limits_exhausted.requested` | The session reached the exhausted-budget flow and needs a user decision before continuing. | `requestId`, `maxAiCredits`, `usedAiCredits` | | ||
| | `session_limits_exhausted.completed` | The 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`: | ||
|
|
||
| ```typescript | ||
| session.on((event) => { | ||
| if (event.type === "session_limits_exhausted.requested") { | ||
| showBudgetDialog({ | ||
| requestId: event.data.requestId, | ||
| maxAiCredits: event.data.maxAiCredits, | ||
| usedAiCredits: event.data.usedAiCredits, | ||
| }); | ||
| } | ||
| }); | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.