import {z} from 'zod' export type BetaUsageReportServiceTier = 'batch' | 'flex' | 'flex_discount' | 'priority' | 'priority_on_demand' | 'standard'; export type BetaMessagesUsageReportContextWindow = '0-200k' | '200k-1M'; export type BetaInferenceGeoFilter = 'global' | 'not_available' | 'us'; export type BetaSpeed = 'standard' | 'fast'; export type BetaMessagesUsageReportGroupBy = 'account_id' | 'api_key_id' | 'context_window' | 'inference_geo' | 'model' | 'service_account_id' | 'service_tier' | 'speed' | 'workspace_id'; export type BetaMessagesUsageReportTimeBucketWidth = '1d' | '1h' | '1m'; export type AnthropicBeta = string | 'message-batches-2024-09-24' | 'prompt-caching-2024-07-31' | 'computer-use-2024-10-22' | 'computer-use-2025-01-24' | 'pdfs-2024-09-25' | 'token-counting-2024-11-01' | 'token-efficient-tools-2025-02-19' | 'output-128k-2025-02-19' | 'files-api-2025-04-14' | 'mcp-client-2025-04-04' | 'mcp-client-2025-11-20' | 'dev-full-thinking-2025-05-14' | 'interleaved-thinking-2025-05-14' | 'code-execution-2025-05-22' | 'extended-cache-ttl-2025-04-11' | 'context-1m-2025-08-07' | 'context-management-2025-06-27' | 'model-context-window-exceeded-2025-08-26' | 'skills-2025-10-02' | 'fast-mode-2026-02-01' | 'output-300k-2026-03-24' | 'user-profiles-2026-03-24' | 'user-profiles-2026-08-18' | 'user-profiles-2026-09-04' | 'advisor-tool-2026-03-01' | 'managed-agents-2026-04-01' | 'cache-diagnosis-2026-04-07' | 'dreaming-2026-04-21' | 'thinking-token-count-2026-05-13' | 'server-side-fallback-2026-06-01' | 'server-side-fallback-2026-07-01' | 'fallback-credit-2026-06-01' | 'fallback-credit-2026-07-01' | 'agent-memory-2026-07-22' | 'mid-conversation-tool-changes-2026-07-01' | 'compact-2026-01-12' | 'computer-use-2025-11-24' | 'mcp-tunnels-2026-06-22' | 'structured-outputs-2025-11-13' | 'task-budgets-2026-03-13' | 'thinking-display-updates-2026-08-18' | 'ce-user-management-2026-07-13' | 'mid-conversation-output-config-2026-07-01' | 'thinking-binding-controls-2026-08-01' | 'mid-conversation-system-clear-at-2026-08-21' | 'compact-2026-09-04' | 'inline-tools-2026-09-15' | 'mcp-client-2026-09-15' | 'ce-plugins-2026-09-01' | 'spend-limit-reads-2026-09-26' | 'telemetry-destinations-2026-08-11' | 'webhooks-2026-10-07' | 'admin-user-sign-out-2026-10-08' | 'message-threads-2026-08-12' | 'analytics-usage-cost-2026-08-24' | 'experimental-claude-tag-2026-10-21'; export type GetApiV1OrganizationsUsageReportMessagesBetaTrueArgs = {limit?: number | undefined, page?: (string | null) | undefined, starting_at: string, ending_at?: (string | null) | undefined, 'api_key_ids[]'?: (Array | null) | undefined, 'workspace_ids[]'?: (Array | null) | undefined, 'account_ids[]'?: (Array | null) | undefined, 'service_account_ids[]'?: (Array | null) | undefined, 'models[]'?: (Array | null) | undefined, 'service_tiers[]'?: (Array | null) | undefined, 'context_window[]'?: (Array | null) | undefined, 'inference_geos[]'?: (Array | null) | undefined, 'speeds[]'?: (Array | null) | undefined, 'group_by[]'?: (Array | null) | undefined, bucket_width?: BetaMessagesUsageReportTimeBucketWidth | undefined, 'anthropic-beta'?: Array | undefined, 'anthropic-version'?: string | undefined}; export type GetApiV1OrganizationsUsageReportMessagesBetaTrueOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const betaCacheCreation = z.object({ephemeral_1h_input_tokens: z.number().int().gte(0), ephemeral_5m_input_tokens: z.number().int().gte(0)}); export const betaServerToolUse = z.object({web_search_requests: z.number().int()}); export const betaUsageReportServiceTier = z.enum(["batch", "flex", "flex_discount", "priority", "priority_on_demand", "standard"]); export const betaMessagesUsageReportContextWindow = z.enum(["0-200k", "200k-1M"]); export const betaInferenceGeoFilter = z.enum(["global", "not_available", "us"]); export const betaSpeed = z.enum(["standard", "fast"]); export const betaMessagesUsageReportItem = z.object({uncached_input_tokens: z.number().int(), cache_creation: betaCacheCreation, cache_read_input_tokens: z.number().int(), output_tokens: z.number().int(), server_tool_use: betaServerToolUse, api_key_id: z.string().nullable(), workspace_id: z.string().nullable(), account_id: z.string().nullable(), service_account_id: z.string().nullable(), model: z.string().nullable(), service_tier: betaUsageReportServiceTier.nullable(), context_window: betaMessagesUsageReportContextWindow.nullable(), inference_geo: betaInferenceGeoFilter.nullable(), speed: betaSpeed.nullable()}); export const betaMessagesUsageReportTimeBucket = z.object({starting_at: z.string(), ending_at: z.string(), results: z.array(betaMessagesUsageReportItem)}); export const betaGetMessagesUsageReportResponse = z.object({data: z.array(betaMessagesUsageReportTimeBucket), has_more: z.boolean(), next_page: z.string().nullable()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Get Messages Usage Report * * GET /v1/organizations/usage_report/messages?beta=true */ export const getApiV1OrganizationsUsageReportMessagesBetaTrue = async (args: GetApiV1OrganizationsUsageReportMessagesBetaTrueArgs, options?: GetApiV1OrganizationsUsageReportMessagesBetaTrueOptions) => { const {baseUrl = "https://api.anthropic.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.page != null) query.append("page", String(args.page)) if (args.starting_at != null) query.append("starting_at", String(args.starting_at)) if (args.ending_at != null) query.append("ending_at", String(args.ending_at)) for (const item of args['api_key_ids[]'] ?? []) query.append("api_key_ids[]", String(item)) for (const item of args['workspace_ids[]'] ?? []) query.append("workspace_ids[]", String(item)) for (const item of args['account_ids[]'] ?? []) query.append("account_ids[]", String(item)) for (const item of args['service_account_ids[]'] ?? []) query.append("service_account_ids[]", String(item)) for (const item of args['models[]'] ?? []) query.append("models[]", String(item)) for (const item of args['service_tiers[]'] ?? []) query.append("service_tiers[]", String(item)) for (const item of args['context_window[]'] ?? []) query.append("context_window[]", String(item)) for (const item of args['inference_geos[]'] ?? []) query.append("inference_geos[]", String(item)) for (const item of args['speeds[]'] ?? []) query.append("speeds[]", String(item)) for (const item of args['group_by[]'] ?? []) query.append("group_by[]", String(item)) if (args.bucket_width != null) query.append("bucket_width", String(args.bucket_width)) if (args['anthropic-beta'] != null) headers.set("anthropic-beta", args['anthropic-beta'].join(',')) if (args['anthropic-version'] != null) headers.set("anthropic-version", String(args['anthropic-version'])) const url = `${baseUrl.replace(/\/+$/, '')}/v1/organizations/usage_report/messages?beta=true${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(betaGetMessagesUsageReportResponse, await res.json()) };