Documentation
¶
Overview ¶
Package grok is an idiomatic Go client SDK for the xAI Grok API.
Create a client with New, then access each API surface through its sub-client:
client := grok.New(grok.WithAPIKey(os.Getenv("XAI_API_KEY")))
resp, err := client.Chat.Create(ctx, &chat.CreateRequest{...})
API surfaces exposed on Client:
- [Client.Chat] — chat completions, streaming, tool use (chat.Client)
- [Client.Responses] — stateful Responses API (responses.Client)
- [Client.Images] — image generation and editing (images.Client)
- [Client.Videos] — async video generation (videos.Client)
- [Client.Voice] — TTS, STT, and realtime voice (voice.Client)
- [Client.Models] — model listing (models.Client)
- [Client.Files] — file upload and download (files.Client)
- [Client.Batches] — async bulk inference (batches.Client)
- Client.NewGRPC — gRPC transport alternative
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type APIError ¶
APIError is returned when the xAI API responds with a non-2xx status code. Use errors.As to extract it from any error returned by this package:
var apiErr *grok.APIError
if errors.As(err, &apiErr) {
fmt.Println(apiErr.StatusCode, apiErr.Message)
}
type Client ¶
type Client struct {
Chat *chat.Client
Responses *responses.Client
Images *images.Client
Videos *videos.Client
Voice *voice.Client
Models *models.Client
Files *files.Client
Batches *batches.Client
// contains filtered or unexported fields
}
Client is the top-level SDK entry point. Use New() to create one.
func New ¶
New creates a Client. If no WithAPIKey option is provided, it reads XAI_API_KEY from env.
Example ¶
package main
import (
"github.com/buckedunicorn/grok"
)
func main() {
// New reads XAI_API_KEY from the environment when WithAPIKey is omitted.
client := grok.New()
_ = client
}
Output:
Example (Options) ¶
package main
import (
"os"
"github.com/buckedunicorn/grok"
)
func main() {
client := grok.New(
grok.WithAPIKey(os.Getenv("XAI_API_KEY")),
grok.WithMaxRetries(3),
grok.WithConvID("session-42"),
)
_ = client
}
Output:
func (*Client) NewGRPC ¶
NewGRPC creates a gRPC client for the xAI API at api.x.ai:443. The gRPC surface mirrors the REST inference endpoints; proto definitions are at https://github.com/xai-org/xai-proto. Additional dial options (e.g. interceptors) may be passed via opts.
func (*Client) WithConvID ¶
WithConvID returns a new Client with the x-grok-conv-id header set for all requests. Use this to scope a conversation for prompt caching without recreating the full client.
Example ¶
package main
import (
"github.com/buckedunicorn/grok"
)
func main() {
client := grok.New()
// Scope a copy of the client to a specific conversation for cache locality.
scoped := client.WithConvID("conv-abc")
_ = scoped
}
Output:
type Option ¶
type Option func(*config)
Option configures the Client.
func WithAPIKey ¶
WithAPIKey sets the xAI API key. Defaults to the XAI_API_KEY environment variable.
func WithBaseURL ¶
WithBaseURL overrides the API base URL (default: https://api.x.ai). The URL must use the https scheme; non-HTTPS values are rejected to prevent silent downgrade of the API key. For testing or trusted internal mirrors that do not speak TLS, combine with WithInsecureBaseURL.
func WithConcurrency ¶
WithConcurrency limits the number of simultaneous in-flight HTTP requests to n. Requests beyond the limit block until a slot is free. 0 means unlimited (default).
func WithConvID ¶
WithConvID sets the x-grok-conv-id header on all requests from this client, which maximises prompt-cache hit rates across consecutive turns.
func WithHTTPClient ¶
WithHTTPClient replaces the default HTTP client.
func WithInsecureBaseURL ¶
func WithInsecureBaseURL() Option
WithInsecureBaseURL permits non-HTTPS base URLs paired with WithBaseURL. Use only for local tests or trusted on-network mirrors. The bearer token is sent in cleartext over plain HTTP.
func WithLogger ¶
WithLogger attaches a slog.Logger that receives request/response events. Successful requests are logged at Info; retries at Warn; failures at Error.
func WithMaxRetries ¶
WithMaxRetries configures automatic retry with exponential backoff for transient errors (429, 500, 502, 503, 504, and network errors). n=0 disables retries (default).
Directories
¶
| Path | Synopsis |
|---|---|
|
Package agents is the structured agent harness built on chat.Client.
|
Package agents is the structured agent harness built on chat.Client. |
|
durable
Package durable provides checkpoint persistence for agents.Runner.
|
Package durable provides checkpoint persistence for agents.Runner. |
|
eval
Package eval scores agents.RunResult values produced by agents.Runner.
|
Package eval scores agents.RunResult values produced by agents.Runner. |
|
harness
Package harness is the opinionated default agent on top of agents.Runner.
|
Package harness is the opinionated default agent on top of agents.Runner. |
|
sandbox
Package sandbox provides sandboxing implementations of the harness Executor contract.
|
Package sandbox provides sandboxing implementations of the harness Executor contract. |
|
tracing
Package tracing exports OpenTelemetry instrumentation for agents.Runner.
|
Package tracing exports OpenTelemetry instrumentation for agents.Runner. |
|
Package batches provides the /v1/batches endpoints for async bulk inference at 50% cost.
|
Package batches provides the /v1/batches endpoints for async bulk inference at 50% cost. |
|
Package cache provides prompt-cache management for xAI Grok models.
|
Package cache provides prompt-cache management for xAI Grok models. |
|
Package chat provides the /v1/chat/completions endpoint (OpenAI-compatible).
|
Package chat provides the /v1/chat/completions endpoint (OpenAI-compatible). |
|
Package council provides multi-agent roundtable and synthesis patterns built on top of the chat completions API.
|
Package council provides multi-agent roundtable and synthesis patterns built on top of the chat completions API. |
|
Package discord provides Discord-compatible patterns for the Grok SDK.
|
Package discord provides Discord-compatible patterns for the Grok SDK. |
|
examples
|
|
|
agents-guardrails
command
agents-guardrails demonstrates input and output guardrails on an Agent.
|
agents-guardrails demonstrates input and output guardrails on an Agent. |
|
agents-handoff
command
agents-handoff demonstrates Handoff, one agent transferring control to another mid-run.
|
agents-handoff demonstrates Handoff, one agent transferring control to another mid-run. |
|
agents-runner
command
agents-runner demonstrates agents.Runner, the structured agent harness that captures a Trajectory of every turn.
|
agents-runner demonstrates agents.Runner, the structured agent harness that captures a Trajectory of every turn. |
|
batches
command
batches demonstrates creating a batch job, adding requests, and iterating results.
|
batches demonstrates creating a batch job, adding requests, and iterating results. |
|
cache
command
cache demonstrates cache.Manager for tracking xAI prompt-cache hit rates.
|
cache demonstrates cache.Manager for tracking xAI prompt-cache hit rates. |
|
chat
command
chat demonstrates a single non-streaming chat completion.
|
chat demonstrates a single non-streaming chat completion. |
|
client-options
command
client-options demonstrates grok.WithLogger, grok.WithMaxRetries, and grok.WithConcurrency working together.
|
client-options demonstrates grok.WithLogger, grok.WithMaxRetries, and grok.WithConcurrency working together. |
|
coding-agent
command
coding-agent demonstrates a sandboxed coding agent: harness gives it filesystem and shell tools, sandbox.DockerExecutor runs every shell command inside an ephemeral python:3.12-alpine container with the workspace mounted read-write.
|
coding-agent demonstrates a sandboxed coding agent: harness gives it filesystem and shell tools, sandbox.DockerExecutor runs every shell command inside an ephemeral python:3.12-alpine container with the workspace mounted read-write. |
|
conversation
command
conversation demonstrates multi-turn chat using chat.Conversation, which automatically tracks message history across turns.
|
conversation demonstrates multi-turn chat using chat.Conversation, which automatically tracks message history across turns. |
|
council
command
council demonstrates council.Roundtable and council.Synthesize.
|
council demonstrates council.Roundtable and council.Synthesize. |
|
decode
command
decode demonstrates chat.Decode[T] for structured JSON output.
|
decode demonstrates chat.Decode[T] for structured JSON output. |
|
discord
command
discord demonstrates the discord.Router, a zero-dependency adapter that manages per-channel chat.Conversations and formats replies for Discord's 2000-character message limit.
|
discord demonstrates the discord.Router, a zero-dependency adapter that manages per-channel chat.Conversations and formats replies for Discord's 2000-character message limit. |
|
durable
command
durable demonstrates checkpoint-and-resume for agents.Runner.
|
durable demonstrates checkpoint-and-resume for agents.Runner. |
|
eval
command
eval demonstrates agents/eval, score a Suite of tasks against an agent and print an aggregate pass@1 / latency / token report.
|
eval demonstrates agents/eval, score a Suite of tasks against an agent and print an aggregate pass@1 / latency / token report. |
|
files
command
files demonstrates uploading, listing (with the All iterator), and cleaning up files.
|
files demonstrates uploading, listing (with the All iterator), and cleaning up files. |
|
full-stack
command
full-stack composes every advanced agent feature into one runnable demo:
|
full-stack composes every advanced agent feature into one runnable demo: |
|
grpc
command
grpc demonstrates making a chat completion via the xAI gRPC API.
|
grpc demonstrates making a chat completion via the xAI gRPC API. |
|
harness
command
harness demonstrates agents/harness, the opinionated default agent preconfigured with planning, filesystem, and subagent tools.
|
harness demonstrates agents/harness, the opinionated default agent preconfigured with planning, filesystem, and subagent tools. |
|
image-edit
command
image-edit modifies an existing image using a text prompt.
|
image-edit modifies an existing image using a text prompt. |
|
images
command
images demonstrates image generation and saves the result URL.
|
images demonstrates image generation and saves the result URL. |
|
memory
command
memory demonstrates in-process and file-backed memory stores used as conversation context.
|
memory demonstrates in-process and file-backed memory stores used as conversation context. |
|
models
command
models lists available language models with their pricing.
|
models lists available language models with their pricing. |
|
queue
command
queue demonstrates the rate-limiting request queue.
|
queue demonstrates the rate-limiting request queue. |
|
responses
command
responses demonstrates the stateful Responses API with multi-turn chaining.
|
responses demonstrates the stateful Responses API with multi-turn chaining. |
|
sandbox
command
sandbox demonstrates wiring agents/sandbox.DockerExecutor into agents/harness so the agent's shell tool runs inside an ephemeral container instead of on the host.
|
sandbox demonstrates wiring agents/sandbox.DockerExecutor into agents/harness so the agent's shell tool runs inside an ephemeral container instead of on the host. |
|
sandboxed-eval
command
sandboxed-eval composes agents/eval with a sandboxed harness: every task runs through harness.NewDefault wired to a sandbox.DockerExecutor, so all code execution happens inside python:3.12-alpine containers.
|
sandboxed-eval composes agents/eval with a sandboxed harness: every task runs through harness.NewDefault wired to a sandbox.DockerExecutor, so all code execution happens inside python:3.12-alpine containers. |
|
server-tools
command
server-tools demonstrates xAI's server-side tools on the Responses API.
|
server-tools demonstrates xAI's server-side tools on the Responses API. |
|
streaming-chat
command
streaming-chat demonstrates token-by-token streaming using SSE.
|
streaming-chat demonstrates token-by-token streaming using SSE. |
|
stt
command
stt transcribes an audio file passed as the first argument.
|
stt transcribes an audio file passed as the first argument. |
|
sugar
command
sugar demonstrates the optional langchain-style helpers, prompt templates, runnable composition, and output parsers, wired into a single typed pipeline.
|
sugar demonstrates the optional langchain-style helpers, prompt templates, runnable composition, and output parsers, wired into a single typed pipeline. |
|
thinking
command
thinking demonstrates chat.ThinkingContent and chat.StripThinking.
|
thinking demonstrates chat.ThinkingContent and chat.StripThinking. |
|
tool-use
command
tool-use demonstrates the RunAgent loop: the model is given two tools (get_weather and add_numbers) and RunAgent handles all tool calls automatically until the model returns a final text answer.
|
tool-use demonstrates the RunAgent loop: the model is given two tools (get_weather and add_numbers) and RunAgent handles all tool calls automatically until the model returns a final text answer. |
|
tracing
command
tracing demonstrates agents/tracing, OpenTelemetry instrumentation that turns every Runner.Run call into a hierarchy of spans an OTel-aware backend (Jaeger, Tempo, Honeycomb, Datadog, etc.) can index and query.
|
tracing demonstrates agents/tracing, OpenTelemetry instrumentation that turns every Runner.Run call into a hierarchy of spans an OTel-aware backend (Jaeger, Tempo, Honeycomb, Datadog, etc.) can index and query. |
|
tts
command
tts synthesizes speech from text and writes the audio to a file.
|
tts synthesizes speech from text and writes the audio to a file. |
|
video-edit
command
video-edit edits an existing video using a text prompt.
|
video-edit edits an existing video using a text prompt. |
|
video-extend
command
video-extend continues an existing video for additional seconds.
|
video-extend continues an existing video for additional seconds. |
|
video-from-image
command
video-from-image animates a still image into a video (image-to-video).
|
video-from-image animates a still image into a video (image-to-video). |
|
video-from-prompt
command
video-from-prompt generates a video from a text prompt and polls until complete.
|
video-from-prompt generates a video from a text prompt and polls until complete. |
|
workflow
command
workflow demonstrates composing Grok calls as workflow Steps.
|
workflow demonstrates composing Grok calls as workflow Steps. |
|
Package files provides the /v1/files endpoints for managing uploaded files.
|
Package files provides the /v1/files endpoints for managing uploaded files. |
|
Package grpc provides a gRPC client for the xAI API.
|
Package grpc provides a gRPC client for the xAI API. |
|
Package images provides the /v1/images/generations and /v1/images/edits endpoints.
|
Package images provides the /v1/images/generations and /v1/images/edits endpoints. |
|
internal
|
|
|
apierr
Package apierr defines the shared API error type for the grok SDK.
|
Package apierr defines the shared API error type for the grok SDK. |
|
idvalidate
Package idvalidate is a tiny shared helper for client-side validation of opaque IDs that get concatenated into request paths.
|
Package idvalidate is a tiny shared helper for client-side validation of opaque IDs that get concatenated into request paths. |
|
multipart
Package multipart builds multipart/form-data request bodies for the SDK's upload endpoints (files, STT).
|
Package multipart builds multipart/form-data request bodies for the SDK's upload endpoints (files, STT). |
|
transport
Package transport is the shared HTTP layer used by every public sub-client (chat, responses, images, ...).
|
Package transport is the shared HTTP layer used by every public sub-client (chat, responses, images, ...). |
|
Package memory provides conversation and agent memory backends.
|
Package memory provides conversation and agent memory backends. |
|
Package models provides access to the xAI model listing endpoints.
|
Package models provides access to the xAI model listing endpoints. |
|
Package prompt provides string templates with {name} variable substitution for chat messages.
|
Package prompt provides string templates with {name} variable substitution for chat messages. |
|
Package queue provides a token-bucket rate limiter for API calls.
|
Package queue provides a token-bucket rate limiter for API calls. |
|
Package responses provides the /v1/responses endpoint (xAI Responses API).
|
Package responses provides the /v1/responses endpoint (xAI Responses API). |
|
Package runnable provides a generic Runnable[In, Out] interface and small combinators (Pipe, Parallel, Branch, Map, Retry) for composing typed pipelines.
|
Package runnable provides a generic Runnable[In, Out] interface and small combinators (Pipe, Parallel, Branch, Map, Retry) for composing typed pipelines. |
|
Package videos provides the /v1/videos/* endpoints.
|
Package videos provides the /v1/videos/* endpoints. |
|
Package voice provides TTS, STT, and realtime voice endpoints.
|
Package voice provides TTS, STT, and realtime voice endpoints. |
|
realtime
Package realtime provides a WebSocket client for the xAI Realtime voice API.
|
Package realtime provides a WebSocket client for the xAI Realtime voice API. |
|
sttstream
Package sttstream provides a WebSocket client for streaming speech-to-text.
|
Package sttstream provides a WebSocket client for streaming speech-to-text. |
|
ttsstream
Package ttsstream provides a WebSocket client for streaming text-to-speech.
|
Package ttsstream provides a WebSocket client for streaming text-to-speech. |
|
Package workflow provides composable building blocks for multi-step AI pipelines.
|
Package workflow provides composable building blocks for multi-step AI pipelines. |