Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ it.effect("takes a hub's reset for an unreset usage_limit, then refreshes the sn
},
};
const lookups: ProviderInstanceId[] = [];
const hubLookups: Array<[string, string, string | null]> = [];
const hubLookups: Array<[string, string | undefined, string | null]> = [];
const instances = Layer.mock(ProviderInstanceRegistry.ProviderInstanceRegistry)({
getInstance: (id) =>
Effect.sync(() => {
Expand Down Expand Up @@ -341,9 +341,9 @@ it.effect("takes a hub's reset for an unreset usage_limit, then refreshes the sn
}).pipe(
Effect.provide(makeLayerTest(layerTestEventSink, instances)),
Effect.provideService(UsageLimitResetFallback, {
resetAt: (driver, endpoint, model) =>
resetAt: (driver, auth, model) =>
Effect.sync(() => {
hubLookups.push([driver, endpoint, model]);
hubLookups.push([driver, auth.endpoint, model]);
return "2026-11-13T18:00:00.000Z";
}),
}),
Expand Down
8 changes: 4 additions & 4 deletions apps/server/src/orchestration-v2/ProviderEventIngestor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -671,15 +671,15 @@ export const layer: Layer.Layer<
? { ...input.event.failure, message: "Killed: out of memory.", code: "oom_kill" }
: input.event.failure;
// A proxied CLI can drop the reset time; the hub still knows when its pool refills.
const endpoint =
const auth =
reported.class === "usage_limit" && reported.resetAt == null
? (yield* Effect.flatMap(
instances.getInstance(input.providerInstanceId),
(instance) => instance?.snapshot.getSnapshot ?? Effect.undefined,
))?.auth.endpoint
))?.auth
: undefined;
const hubResetAt =
endpoint === undefined
auth?.endpoint === undefined
? null
: yield* Effect.gen(function* () {
// The failing run's model, which may differ from a selection changed mid-turn.
Expand All @@ -696,7 +696,7 @@ export const layer: Layer.Layer<
}).pipe(
Effect.orElseSucceed(() => null),
Effect.flatMap((model) =>
usageLimitResets.resetAt(input.event.driver, endpoint, model),
usageLimitResets.resetAt(input.event.driver, auth, model),
),
);
const failure = hubResetAt === null ? reported : { ...reported, resetAt: hubResetAt };
Expand Down
14 changes: 9 additions & 5 deletions apps/server/src/provider/ClaudeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -829,9 +829,8 @@ export const checkClaudeProviderStatus = Effect.fn("checkClaudeProviderStatus")(
const baseUrl =
(yield* readClaudeSettingsEnv(claudeSettings, cwd, resolvedEnvironment)).ANTHROPIC_BASE_URL ??
resolvedEnvironment.ANTHROPIC_BASE_URL;
const customApiEndpoint = customClaudeApiEndpointHost(baseUrl) !== undefined;
const endpoint = customApiEndpoint ? URL.parse(baseUrl?.trim() ?? "")?.origin : undefined;
if (!customApiEndpoint && claudeAuthStatus(capabilities) === "unauthenticated") {
const endpoint = customClaudeApiEndpointHost(baseUrl) ? URL.parse(baseUrl?.trim() ?? "") : null;
if (!endpoint && claudeAuthStatus(capabilities) === "unauthenticated") {
const path = yield* Path.Path;
const configDir = (yield* makeClaudeEnvironment(claudeSettings, resolvedEnvironment))
.CLAUDE_CONFIG_DIR;
Expand Down Expand Up @@ -859,8 +858,13 @@ export const checkClaudeProviderStatus = Effect.fn("checkClaudeProviderStatus")(
// A proxy's ANTHROPIC_AUTH_TOKEN reads as an API key; the endpoint is what the token is for.
const authMetadata =
apiProviderAuthMetadata(capabilities.apiProvider) ??
(customApiEndpoint
? { type: "customEndpoint", label: "Custom API endpoint", ...(endpoint ? { endpoint } : {}) }
(endpoint
? {
type: "customEndpoint",
label: "Custom API endpoint",
endpoint: endpoint.origin,
endpointPath: endpoint.pathname,
}
: undefined) ??
claudeAuthMetadata({
subscriptionType: capabilities.subscriptionType,
Expand Down
21 changes: 15 additions & 6 deletions apps/server/src/provider/CodexProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ it("reports native quota only for an OpenAI login on a native route", () => {
const chatgpt = { type: "chatgpt", email: "a@example.com", planType: "pro" } as const;
const hub = {
model_provider: "hub",
model_providers: { hub: { base_url: "https://hub.example.com/v1" } },
model_providers: { hub: { base_url: "https://hub.example.com/team/v1" } },
};
const cases = [
["default route", chatgpt, true, {}, true],
Expand All @@ -332,11 +332,20 @@ it("reports native quota only for an OpenAI login on a native route", () => {
);
}
// The custom endpoint is what a usage-limit hub is matched against.
assert.strictEqual(codexCustomEndpoint(hub), "https://hub.example.com");
assert.strictEqual(
codexCustomEndpoint({ openai_base_url: "http://127.0.0.1:8317/v1" }),
"http://127.0.0.1:8317",
);
assert.deepStrictEqual(codexCustomEndpoint(hub), {
endpoint: "https://hub.example.com",
endpointPath: "/team/v1",
endpointPathWithoutApiVersion: "/team",
});
assert.deepStrictEqual(codexCustomEndpoint({ openai_base_url: "http://127.0.0.1:8317/v1/" }), {
endpoint: "http://127.0.0.1:8317",
endpointPath: "/v1/",
endpointPathWithoutApiVersion: "/",
});
assert.deepStrictEqual(codexCustomEndpoint({ openai_base_url: "https://hub.example.com/team" }), {
endpoint: "https://hub.example.com",
endpointPath: "/team",
});
assert.strictEqual(codexCustomEndpoint({ openai_base_url: "https://chatgpt.com/x" }), undefined);
assert.strictEqual(codexCustomEndpoint({}), undefined);
});
24 changes: 14 additions & 10 deletions apps/server/src/provider/CodexProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,19 @@ function routesToNativeOpenai(config: unknown): boolean {
);
}

/** The origin of the custom endpoint Codex sends model requests to, if it uses one. */
export function codexCustomEndpoint(config: unknown): string | undefined {
/** The custom endpoint Codex sends model requests to, without URL credentials. */
export function codexCustomEndpoint(config: unknown) {
const baseUrl = codexRouteBaseUrl(config);
const url = baseUrl ? URL.parse(baseUrl) : null;
return url && /^https?:$/.test(url.protocol) && !routesToNativeOpenai(config)
? url.origin
: undefined;
if (!url || !/^https?:$/.test(url.protocol) || routesToNativeOpenai(config)) return undefined;
const pathWithoutVersion = url.pathname.replace(/\/v1\/*$/, "");
return {
endpoint: url.origin,
endpointPath: url.pathname,
...(pathWithoutVersion !== url.pathname
? { endpointPathWithoutApiVersion: pathWithoutVersion || "/" }
: {}),
};
}

// A stored login does not mean this provider's traffic spends that login's quota.
Expand Down Expand Up @@ -156,7 +162,7 @@ const CODEX_PRESENTATION = {

export interface CodexAppServerProviderSnapshot {
readonly account: CodexSchema.V2GetAccountResponse;
readonly endpoint?: string | undefined;
readonly customEndpoint?: ReturnType<typeof codexCustomEndpoint>;
readonly nativeUsage?: boolean;
readonly rateLimits?: CodexRateLimitsProbe;
readonly version: string | undefined;
Expand Down Expand Up @@ -811,7 +817,7 @@ const probeCodexAppServerProvider = Effect.fn("probeCodexAppServerProvider")(fun
const cyberAccess = Option.getOrUndefined(decodeCodexCyberAccessConfig(config));
return {
account: accountResponse,
endpoint: config ? codexCustomEndpoint(config) : undefined,
customEndpoint: config ? codexCustomEndpoint(config) : undefined,
nativeUsage,
...(rateLimits ? { rateLimits } : {}),
version,
Expand Down Expand Up @@ -1176,9 +1182,7 @@ export const checkCodexProviderStatus = Effect.fn("checkCodexProviderStatus")(fu
installed: true,
version: snapshot.version ?? null,
status: snapshot.models === undefined ? "warning" : accountStatus.status,
auth: snapshot.endpoint
? { ...accountStatus.auth, endpoint: snapshot.endpoint }
: accountStatus.auth,
auth: { ...accountStatus.auth, ...snapshot.customEndpoint },
...(accountStatus.message
? { message: accountStatus.message }
: snapshot.models === undefined
Expand Down
12 changes: 10 additions & 2 deletions apps/server/src/provider/ProviderRegistry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4679,7 +4679,9 @@ it.layer(

it.effect.each([
{
settings: { env: { ANTHROPIC_BASE_URL: "https://hub.example.ts.net:8317" } },
settings: { env: { ANTHROPIC_BASE_URL: "https://hub.example.ts.net:8317/team" } },
endpoint: "https://hub.example.ts.net:8317",
endpointPath: "/team",
baseUrl: "https://api.anthropic.com",
type: "customEndpoint",
label: "Custom API endpoint",
Expand All @@ -4689,16 +4691,20 @@ it.layer(
baseUrl: "https://proxy.example",
type: "apiKey",
label: "Claude API Key",
endpoint: undefined,
endpointPath: undefined,
},
{
settings: {},
baseUrl: "https://proxy.example",
endpoint: "https://proxy.example",
endpointPath: "/",
type: "customEndpoint",
label: "Custom API endpoint",
},
])(
"uses the settings endpoint before the process endpoint: $type / $baseUrl",
({ settings, baseUrl, type, label }) =>
({ settings, baseUrl, type, label, endpoint, endpointPath }) =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
Expand Down Expand Up @@ -4728,6 +4734,8 @@ it.layer(
assert.strictEqual(status.status, "ready");
assert.strictEqual(status.auth.type, type);
assert.strictEqual(status.auth.label, label);
assert.strictEqual(status.auth.endpoint, endpoint);
assert.strictEqual(status.auth.endpointPath, endpointPath);
}).pipe(
Effect.provide(
layerMockSpawner((args) => {
Expand Down
21 changes: 18 additions & 3 deletions apps/server/src/usage/UsageLimitSources.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,18 @@ it.each([
it.effect("the reset fallback refreshes its hub, and a stalled hub cannot hold the failure", () =>
Effect.gen(function* () {
yield* TestClock.setTime(NOW);
const roomLeft = { ...hub([{ windows: [spent("five_hour", 2)] }]), origin: "http://hub.test" };
const origin = "http://hub.test";
const roomLeft = {
...hub([{ windows: [spent("five_hour", 2)] }]),
origin,
endpoint: `${origin}/team`,
};
const otherRoute = {
...hub([{ windows: [] }]),
id: UsageLimitSourceId.make("other"),
origin,
endpoint: `${origin}/other`,
};
const cached = {
...roomLeft,
accounts: roomLeft.accounts.map((account) => ({
Expand All @@ -381,11 +392,15 @@ it.effect("the reset fallback refreshes its hub, and a stalled hub cannot hold t
};
let current: ReadonlyArray<UsageLimitSourceSnapshot> = [cached];
let refresh: Effect.Effect<void> = Effect.sync(() => {
current = [roomLeft];
current = [roomLeft, otherRoute];
});
const lookup = UsageLimitSources.UsageLimitResetFallback.pipe(
Effect.flatMap((fallback) =>
fallback.resetAt(ProviderDriverKind.make("claudeAgent"), "http://hub.test", null),
fallback.resetAt(
ProviderDriverKind.make("claudeAgent"),
{ status: "authenticated", endpoint: origin, endpointPath: "/team" },
null,
),
),
Effect.provide(
UsageLimitSources.layerResetFallback.pipe(
Expand Down
10 changes: 5 additions & 5 deletions apps/server/src/usage/UsageLimitSources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import {
type UsageLimitSourceConsumeResetCreditInput,
type ProviderConsumeResetCreditResult,
type ProviderDriverKind,
type ServerProviderAuth,
type ServerSettings,
UsageLimitSourceConfig,
settingsSecretLiteral,
type UsageLimitSourceId,
type UsageLimitSourceSnapshot,
} from "@t3tools/contracts";
import { hubUsageSources } from "@t3tools/shared/usageLimits";
import { resolveServerBackgroundActivitySettings } from "@t3tools/shared/backgroundActivitySettings";
import * as Context from "effect/Context";
import * as DateTime from "effect/DateTime";
Expand Down Expand Up @@ -279,7 +281,7 @@ export function hubPoolResetAt(
export class UsageLimitResetFallback extends Context.Reference<{
readonly resetAt: (
driver: ProviderDriverKind,
endpoint: string,
auth: ServerProviderAuth,
model: string | null,
) => Effect.Effect<string | null>;
}>("t3/usage/UsageLimitResetFallback", {
Expand All @@ -291,11 +293,9 @@ export const layerResetFallback = Layer.effect(
Effect.gen(function* () {
const sources = yield* UsageLimitSources;
return {
resetAt: Effect.fn("UsageLimitResetFallback.resetAt")(function* (driver, endpoint, model) {
resetAt: Effect.fn("UsageLimitResetFallback.resetAt")(function* (driver, auth, model) {
// Only the hub at the instance's endpoint serves its turns.
const atEndpoint = Effect.map(sources.current, (current) =>
current.filter((source) => source.origin === endpoint),
);
const atEndpoint = Effect.map(sources.current, (current) => hubUsageSources(current, auth));
if ((yield* atEndpoint).length === 0) return null;
// The pool usually ran out after the last poll, so the cached accounts still show room.
yield* sources.refresh.pipe(Effect.timeout("5 seconds"), Effect.ignore);
Expand Down
13 changes: 4 additions & 9 deletions apps/web/src/components/chat/ThreadDetailsUsage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {
UsageLimitSourceSnapshots,
} from "@t3tools/contracts";
import {
collectProviderUsageLimits,
instanceUsageLimits,
type LimitPace,
paceOf,
paceRemainingPercent,
Expand Down Expand Up @@ -51,15 +51,10 @@ export function ThreadDetailsUsage({
const m = useMessages();
const nowMinute = useNowMinute();
const now = Date.parse(`${nowMinute}:00Z`);
const report =
providerInstanceId !== null
? collectProviderUsageLimits(providerInstanceId, providers, sources, now)
: null;
// Only this instance's data: the report keeps one instance per login, and any other entry may be
// a different login or workspace, so fall back to the instance's own snapshot.
const limits =
report?.accounts.find((entry) => entry.instanceId === providerInstanceId)?.limits ??
providers.find((provider) => provider.instanceId === providerInstanceId)?.usageLimits;
providerInstanceId !== null
? instanceUsageLimits(providerInstanceId, providers, sources, now)
: undefined;
if (!limits) return null;
const notice = limitsNotice(limits, m);

Expand Down
9 changes: 6 additions & 3 deletions docs/user/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,12 @@ The hub's Codex, Claude, xAI (shown as Grok) and Antigravity accounts appear und
accounts for other providers are not read. Codex accounts show banked reset credits; select an
account and choose **Use reset** to redeem one. No hub plugin is required.

This connection supplies usage information; configure
the provider separately to send agent requests through the hub. Remove the hub from the same
settings section when you no longer need it.
Configure the provider separately to send agent requests through the hub. When the Codex or Claude
instance's API base URL points to the hub, the thread details card shows the hub's pooled limits for
that provider. Use the same hostname, port, and reverse proxy path in both settings. A Codex API
base URL can include the OpenAI `/v1` suffix. When that URL matches both a hub with the suffix and
a hub without it, the card uses the instance's own limits. Remove the hub from the same settings
section when you no longer need it.

## Subscription usage widget

Expand Down
4 changes: 4 additions & 0 deletions packages/contracts/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ export const ServerProviderAuth = Schema.Struct({
workspaceId: Schema.optional(TrimmedNonEmptyString),
/** Origin of the custom endpoint the instance sends its requests to. */
endpoint: Schema.optional(TrimmedNonEmptyString),
/** Path of the custom API base URL. A slash denotes the root path. */
endpointPath: Schema.optional(TrimmedNonEmptyString),
/** Alternative hub mount when the API base URL ends in a provider API version. */
endpointPathWithoutApiVersion: Schema.optional(TrimmedNonEmptyString),
});
export type ServerProviderAuth = typeof ServerProviderAuth.Type;

Expand Down
Loading
Loading