Skip to content

feat: add OpenAI Batch API support for worker calls#309

Merged
BYK merged 2 commits into
mainfrom
byk/openai-batch-api
May 14, 2026
Merged

feat: add OpenAI Batch API support for worker calls#309
BYK merged 2 commits into
mainfrom
byk/openai-batch-api

Conversation

@BYK
Copy link
Copy Markdown
Owner

@BYK BYK commented May 14, 2026

Summary

Closes #264.

  • Dual-provider batch queue: Worker calls (distillation, curation, consolidation) now route through the correct batch API based on upstream model provider — Anthropic Messages Batches API or OpenAI Batch API — achieving 50% cost savings for both.
  • BatchProvider strategy interface in batch-queue.ts: Anthropic provider (existing logic, extracted) and new OpenAI provider (JSONL file upload → /v1/batches → poll → download results). Items grouped at flush time by (authKey, providerID).
  • Provider-aware llm-adapter.ts: Accepts { anthropic, openai } upstreams object, branches on model.providerID for wire format (Anthropic Messages vs OpenAI Chat Completions). normalizeOpenAIUsage() maps OpenAI usage to Anthropic shape.

Test plan

  • Existing Anthropic batch tests updated for new API surface (all pass)
  • New tests: OpenAI provider grouping, failure fallback to sync, full batch lifecycle (upload → create → poll → resolve), JSONL format verification, normalizeOpenAIUsage coverage
  • bun run typecheck — 0 errors, bun test — 1385 pass / 0 fail, bun run build — clean

BYK added 2 commits May 14, 2026 12:20
Add dual-provider batch queue strategy so worker calls (distillation,
curation, consolidation) route through the correct batch API based on
the upstream model's provider — Anthropic Messages Batches or OpenAI
Batch API — achieving 50% cost savings for both providers.

- llm-adapter.ts: accept { anthropic, openai } upstreams object, branch
  on model.providerID for request/response format, add
  normalizeOpenAIUsage() helper
- batch-queue.ts: extract BatchProvider interface, implement Anthropic
  and OpenAI providers (JSONL upload → batch create → poll → download),
  group flush by (authKey, providerID)
- pipeline.ts + CLI callers: pass both upstream URLs
- Tests: OpenAI provider grouping, failure fallback, full lifecycle,
  JSONL format verification, normalizeOpenAIUsage coverage
- Use authFingerprint() in groupKey instead of raw credential value
  to avoid accidental exposure via logs/Sentry breadcrumbs
- submit() now returns 'auth-error' for 401/403 (permanent disable)
  vs null for transient failures (fallback only, no session disable)
- Remove 'cancelling' from OpenAI terminal states — it's transitional
  and should keep polling until it reaches 'cancelled'
- Fix empty system string producing '""' instead of undefined/[]
- Remove dead 'reject' field from PendingRequest (never called)
@BYK BYK merged commit 48810cf into main May 14, 2026
13 of 14 checks passed
@BYK BYK deleted the byk/openai-batch-api branch May 14, 2026 12:39
@craft-deployer craft-deployer Bot mentioned this pull request May 14, 2026
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(gateway): OpenAI Batch API support for worker calls

1 participant