Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors
TypeScript a2aai-agentsai-gatewayanthropicclaudeclaude-codeclinecodexcopilotcursordeepseekfree-aigeminikimillm-gatewaymcpopenaiopenai-proxyqwentoken-saver
import { withConnectionSpeedStats } from "@/lib/usage/connectionSpeedStats";
import { fetchAndPersistProviderLimits } from "@/lib/usage/providerLimits";
/**
* GET /api/usage/[connectionId] - Get live usage data for a specific connection
* and persist the refreshed Provider Limits cache.
*
* This is the on-demand, per-connection path (the dashboard quota page fetches
* only the connections it shows through here, not all of them at once). It opts
* into refreshing rotating-refresh providers (Codex/OpenAI) so an account with an
* expired access_token still surfaces live quota — made cascade-safe by
* `serializeRefresh` (one token mint at a time per Auth0 group). The bulk
* scheduler keeps the #3019 behaviour of never refreshing rotating providers.
*
* The response also carries `speed` (recent decode tok/s + TTFT for this
* connection), computed at read time and fail-open; it is not persisted.
*/
export async function GET(
_request: Request,
{ params }: { params: Promise<{ connectionId: string }> }
) {
try {
const { connectionId } = await params;
const { usage } = await fetchAndPersistProviderLimits(connectionId, "manual", {
allowRotatingRefresh: true,
});
return Response.json(withConnectionSpeedStats(connectionId, usage));
} catch (error) {
const status =
typeof (error as { status?: unknown })?.status === "number"
? (error as { status: number }).status
: 500;
const message = (error as Error)?.message || "Failed to fetch usage";
console.error("[Usage API] Error fetching usage:", error);
return Response.json({ error: message }, { status });
}
}