Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/CodexEventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ export class CodexEventHandler {
));
case "account/rateLimits/updated":
this.handleRateLimitsUpdated(notification.params);
return null;
return this.createUsageUpdateFromState();
case "account/updated":
this.onAccountUpdated?.(notification.params);
return null;
Expand Down Expand Up @@ -1201,7 +1201,10 @@ export class CodexEventHandler {

private createUsageUpdate(params: ThreadTokenUsageUpdatedNotification): UpdateSessionEvent | null {
this.handleTokenUsageUpdated(params);
return this.createUsageUpdateFromState();
}

private createUsageUpdateFromState(): UpdateSessionEvent | null {
const used = this.sessionState.lastTokenUsage?.totalTokens;
const size = this.sessionState.modelContextWindow;
if (used == null || size == null || size <= 0) {
Expand All @@ -1212,6 +1215,16 @@ export class CodexEventHandler {
sessionUpdate: "usage_update",
used,
size,
...(this.sessionState.rateLimits
? {
_meta: {
"_codex/rateLimits": Array.from(this.sessionState.rateLimits.values(), (entry) => ({
...entry.snapshot,
limitId: entry.limitId,
})),
},
}
: {}),
};
}

Expand Down
84 changes: 84 additions & 0 deletions src/__tests__/CodexACPAgent/data/rate-limits-usage-updates.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
[
{
"method": "sessionUpdate",
"args": [
{
"sessionId": "test-session-id",
"update": {
"sessionUpdate": "usage_update",
"used": 1000,
"size": 128000,
"_meta": {
"_codex/rateLimits": [
{
"limitId": "standard-limit",
"limitName": "Standard",
"primary": {
"usedPercent": 30,
"resetsAt": 1800000000,
"windowDurationMins": 300
},
"secondary": null,
"credits": null,
"individualLimit": null,
"spendControlReached": null,
"planType": null,
"rateLimitReachedType": null,
"normalModelSlug": null
}
]
}
}
}
]
},
{
"method": "sessionUpdate",
"args": [
{
"sessionId": "test-session-id",
"update": {
"sessionUpdate": "usage_update",
"used": 1000,
"size": 128000,
"_meta": {
"_codex/rateLimits": [
{
"limitId": "standard-limit",
"limitName": "Standard",
"primary": {
"usedPercent": 30,
"resetsAt": 1800000000,
"windowDurationMins": 300
},
"secondary": null,
"credits": null,
"individualLimit": null,
"spendControlReached": null,
"planType": null,
"rateLimitReachedType": null,
"normalModelSlug": null
},
{
"limitId": "fast-limit",
"limitName": "Fast",
"primary": {
"usedPercent": 50,
"resetsAt": 1800500000,
"windowDurationMins": 60
},
"secondary": null,
"credits": null,
"individualLimit": null,
"spendControlReached": null,
"planType": null,
"rateLimitReachedType": null,
"normalModelSlug": null
}
]
}
}
}
]
}
]
45 changes: 44 additions & 1 deletion src/__tests__/CodexACPAgent/token-usage-events.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import type { ServerNotification } from '../../app-server';
import { createCodexMockTestFixture, createTestSessionState, type CodexMockTestFixture } from '../acp-test-utils';
import type { TokenUsageBreakdown } from '../../app-server/v2';
import type { RateLimitSnapshot, TokenUsageBreakdown } from '../../app-server/v2';

function createTokenUsageNotification(
sessionId: string,
Expand All @@ -21,6 +21,13 @@ function createTokenUsageNotification(
};
}

function createRateLimitsNotification(rateLimits: RateLimitSnapshot): ServerNotification {
return {
method: 'account/rateLimits/updated',
params: { rateLimits },
};
}

describe('Token Usage Events', () => {
let mockFixture: CodexMockTestFixture;
const sessionId = 'test-session-id';
Expand Down Expand Up @@ -242,6 +249,42 @@ describe('Token Usage Events', () => {
await expect(`${JSON.stringify(events, null, 2)}\n`).toMatchFileSnapshot('data/token-usage-session-update-multiple.json');
});

it('should emit stored rate limits on usage updates', async () => {
const events = await setupPromptAndReturnEvents([
createRateLimitsNotification({
limitId: 'standard-limit',
limitName: 'Standard',
primary: { usedPercent: 30, resetsAt: 1_800_000_000, windowDurationMins: 300 },
secondary: null,
credits: null,
individualLimit: null,
spendControlReached: null,
planType: null,
rateLimitReachedType: null,
normalModelSlug: null,
}),
createTokenUsageNotification(sessionId, {
total: { totalTokens: 1000, inputTokens: 800, cachedInputTokens: 0, cacheWriteInputTokens: 0, outputTokens: 200, reasoningOutputTokens: 0 },
last: { totalTokens: 1000, inputTokens: 800, cachedInputTokens: 0, cacheWriteInputTokens: 0, outputTokens: 200, reasoningOutputTokens: 0 },
modelContextWindow: 128000,
}),
createRateLimitsNotification({
limitId: 'fast-limit',
limitName: 'Fast',
primary: { usedPercent: 50, resetsAt: 1_800_500_000, windowDurationMins: 60 },
secondary: null,
credits: null,
individualLimit: null,
spendControlReached: null,
planType: null,
rateLimitReachedType: null,
normalModelSlug: null,
}),
])();

await expect(`${JSON.stringify(events, null, 2)}\n`).toMatchFileSnapshot('data/rate-limits-usage-updates.json');
});

it('should skip usage_update when model context window is unavailable', async () => {
const events = await setupPromptAndReturnEvents([
createTokenUsageNotification(sessionId, {
Expand Down