feat(xai): xai_batch_chat tool — submit many completions to xAI Batch API - #23333
Closed
Julientalbot wants to merge 4 commits into
Closed
feat(xai): xai_batch_chat tool — submit many completions to xAI Batch API#23333Julientalbot wants to merge 4 commits into
Julientalbot wants to merge 4 commits into
Conversation
…atch API
xAI Batch API processes large volumes of chat completions asynchronously
with reduced pricing and higher rate limits than the synchronous endpoint.
Most batches complete within 24h per xAI's SLA.
Lifecycle: POST /v1/batches (create empty batch with name) → POST
/v1/batches/{id}/requests (add chat-completion requests inline, max 25MB
per request) → GET /v1/batches/{id} (poll state counters until num_pending
== 0) → GET /v1/batches/{id}/results (paginated retrieval).
Changes:
- tools/xai_batch_tool.py: self-contained tool implementation
- xai_batch_chat(requests, name, model, wait, max_wait_seconds,
poll_interval_seconds) — one entry point covering submit + add +
optional poll + paginated retrieval
- Each request dict supports prompt | messages, system, model,
request_id (auto-generated if absent), extra_body
- Per-request model overrides the tool-level default
- wait=True (default): blocks until done, returns results in
submission order; wait=False: returns batch_id immediately
so the caller can resume polling later
- Configurable via config.yaml under xai_batch:
{model, max_wait_seconds, poll_interval_seconds}
- Reuses tools.xai_http.hermes_xai_user_agent for User-Agent
- Self-registers via tools.registry.registry.register
- tests/tools/test_xai_batch_tool.py: 19 unit tests with a scripted
httpx.request fake covering
- check_xai_batch_requirements (with/without API key)
- schema (required requests, optional params)
- argument validation (empty list, missing key, missing prompt,
negative max_wait)
- submit semantics (create + add wiring, default model, per-request
model, messages overrides prompt, 4xx error)
- wait=False (returns after submit + state)
- poll semantics (pending → 0, timeout)
- results pagination (multi-page walk, missing-result fallback)
- headers (Authorization Bearer, User-Agent prefix on every call)
- toolsets.py: add xai_batch_chat to _HERMES_CORE_TOOLS and new
xai_batch toolset
- hermes_cli/tools_config.py: add xai_batch to CONFIGURABLE_TOOLSETS
- tests/tools/test_registry.py: add tools.xai_batch_tool to manual
builtin tool set snapshot
Pairs naturally with NousResearch#23329 (xai_deferred_chat) — deferred handles a single
long completion, batch handles many short-to-medium ones in parallel.
Requires XAI_API_KEY in ~/.hermes/.env.
This was referenced May 10, 2026
Contributor
|
Thanks Julien. We're going to pass on this one. xAI's Batch API is an offline cost-optimization for users running large jobs — not really an agent-facing runtime tool. Exposing it as a model tool would add schema bloat for a capability that's better used as a CLI command or external script. If you want batch processing inside Hermes, the closer fit is the existing infrastructure. Closing with appreciation. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
xai_batch_chat— a tool that wraps xAI's Batch API end-to-end (POST /v1/batches→ add inline requests → poll → paginated retrieval) behind a single sync entry point. Use it to process dozens-to-thousands of chat completions in one shot at reduced pricing and with higher rate limits than the synchronous endpoint, with up to a 24h SLA per xAI.Pairs with #23329 (
xai_deferred_chat): deferred handles one long completion, batch handles many short-to-medium ones in parallel.Why
The Batch API is the only xAI surface where the cost / rate-limit math actually works out for large evaluation, classification, or backfill workloads. Hermes' autonomous mode and delegation features can already orchestrate long-running tasks, but every call still hits the synchronous endpoint with full per-minute rate limits. With this tool an agent (or a cron-scheduled hermes session) can fan out, e.g., a 5 000-row classification job in one POST and walk away.
Changes (5 files, +820/-0)
tools/xai_batch_tool.py(new, ~440 LoC)Single public entry point:
prompt(with optionalsystem) or a fullmessageslist.modeloverrides the tool-level default.request_idsurvives round-trip; missing IDs are auto-generated asreq-{i:05d}-{hex}.wait=Falsereturns immediately after submit + add + state-fetch — useful for fire-and-forget patterns where the caller resumes polling later (e.g. cron-driven).config.yaml:tools.xai_http.hermes_xai_user_agentforUser-Agent.tools.registry.registry.registerwithcheck_fngating onXAI_API_KEY.tests/tools/test_xai_batch_tool.py(new, ~340 LoC)19 unit tests using a scripted
httpx.requestfake (no real network):check_xai_batch_requirementsreturns the right state with/withoutXAI_API_KEY; schema advertises required + optional params correctly.requests, missing API key, requests missing bothpromptandmessages, negativemax_wait_seconds.batch_request_id,batch_request: {method, url, body}); default model resolution; per-request model overrides;messagesoverridesprompt; HTTP 4xx surfaces.wait=False: returns after submit + state-fetch; no poll, no results.num_pendingfrom 2 → 1 → 0 then proceeds; timeout via mockedtime.monotonic.pagination_token; missing results yieldresponse: Nonefor the unfoundrequest_id(graceful degradation).Authorization: Bearer <key>andUser-Agent: Hermes-Agent/<version>on every HTTP call.Wiring
toolsets.py:xai_batch_chatadded to_HERMES_CORE_TOOLS; newxai_batchtoolset.hermes_cli/tools_config.py: newxai_batchentry inCONFIGURABLE_TOOLSETS.tests/tools/test_registry.py:tools.xai_batch_tooladded to the manual snapshot list (same convention as feat(xai): add x_search tool — search X via xAI Responses API #14541, feat(xai): add video generation tool — generate/edit/extend via xAI grok-imagine-video #14543, feat(xai): xai_deferred_chat tool — long-running completions via submit + poll #23329).Validation
pytest tests/tools/test_xai_batch_tool.py tests/tools/test_registry.py→ 50/50 passing locally on top of currentmain.Backward compatibility
toolsets.py,tools_config.py, and the registry snapshot list.check_fn-gated onXAI_API_KEY— invisible to users without an xAI key.xai_batchis not in any default toolset roster; users opt in viatools_configor by enabling the toolset in their profile.Scope deliberately not in this PR
POST /v1/batches/{id}/requestspath only — the file path requires Files API plumbing that doesn't currently exist anywhere in the codebase.responsefrom the result envelope, which works for chat completions; a richer adapter that surfacesimage_response.url/video_response.url/usageis a clean follow-up.xai_batch_chatis a one-shot orchestration tool. Exposingxai_batch_cancel(batch_id),xai_batch_list(), andxai_batch_get(batch_id)as separate tools is straightforward but would inflate this PR; happy to do those follow-ups.