1
xAI Grok Adapter
Shinwoo PARK edited this page 2026-05-11 17:22:59 +09:00

xAI Grok Adapter

BoxBrain ships with a ready-made xAI Grok adapter set so you can get started without writing your own provider glue first.

What it includes

  • createGrokTextModelAdapter
  • createGrokStructuredModelAdapter
  • createGrokImageModelAdapter
  • createGrokAdapters

Provider identifier used by the adapter:

'xai-grok'

Default base URL:

https://api.x.ai/v1

Bundle setup

import { createGrokAdapters } from 'boxbrain';

const grok = createGrokAdapters({
  apiKey: process.env.XAI_API_KEY!,
  textModel: 'grok-4.3-mini',
  structuredModel: 'grok-4.3',
  imageModel: 'grok-imagine-image-quality',
});

Text adapter

The text adapter sends requests to:

POST /chat/completions

It builds a standard message array from:

  • optional system
  • required prompt

Structured-output adapter

The structured adapter also uses:

POST /chat/completions

Behavior:

  • if request.schema exists, it sends response_format: { type: 'json_schema', ... }
  • otherwise, it falls back to response_format: { type: 'json_object' }
  • the returned message content is parsed as JSON

This is the adapter used by the current BoxBrain persona, schedule, memory-selection, and conversation-plan flows.

Image adapter

The image adapter sends requests to:

POST /images/generations

It maps BoxBrain-neutral aspect ratios to xAI payload values like this:

BoxBrain xAI
square 1:1
portrait 3:4
landscape 16:9

Advanced options

Both the per-capability factories and the bundle helper support:

  • apiKey
  • model name(s)
  • baseUrl?
  • fetch?
  • extraHeaders?

That makes the adapter easy to test or route through custom gateways.

Testing approach used in BoxBrain

The adapter is tested with injected fetch functions instead of a vendor SDK. The test suite asserts:

  • URL
  • HTTP method
  • auth headers
  • model selection
  • chat messages
  • JSON-schema response format
  • image aspect-ratio mapping

This keeps the adapter lightweight and deterministic.