grok

package module
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 7, 2026 License: MIT Imports: 15 Imported by: 0

README

grok

An idiomatic, batteries-included Go client SDK for the xAI Grok API.

import "github.com/buckedunicorn/grok"
go get github.com/buckedunicorn/grok

Quick start

package main

import (
    "context"
    "fmt"
    "os"

    "github.com/buckedunicorn/grok"
    "github.com/buckedunicorn/grok/chat"
)

func main() {
    client := grok.New(grok.WithAPIKey(os.Getenv("XAI_API_KEY")))

    comp, err := client.Chat.Create(context.Background(), &chat.CreateRequest{
        Model: "grok-4-1-fast-non-reasoning",
        Messages: []chat.Message{
            {Role: "user", Content: "What is the speed of light?"},
        },
    })
    if err != nil {
        panic(err)
    }
    fmt.Println(comp.Choices[0].Message.Content)
}

Package map

The root client (grok.New(...)) wires up sub-clients for every API surface. Import them as you need them.

Package Endpoint Purpose
chat POST /v1/chat/completions OpenAI-compatible chat completions, streaming, tool use, structured output, conversation helper
responses POST /v1/responses The newer stateful Responses API, server-side tools (web_search, x_search, code_interpreter)
images POST /v1/images/generations, /edits Text-to-image and image-to-image
videos POST /v1/videos/{generations,edits,extensions} Async video generation, with polling helper
voice TTS, STT, realtime voice (HTTP and WebSocket) Text-to-speech, speech-to-text, bidirectional voice
models GET /v1/models and friends List and inspect available models
files /v1/files Upload, download, list, delete files
batches /v1/batches Asynchronous bulk inference at reduced cost
grpc gRPC transport Lower-overhead alternative to REST for high-throughput workloads
Higher-level helpers built on top of the API surface
Package Use it for
agents Structured agent runtime: Agent, Runner, Tool, Handoff, Session, Guardrails, observable Trajectory
agents/harness Opinionated default agent with planning, FS, shell, and subagent tools pre-wired
agents/durable Checkpoint persistence (MemoryStore, FileStore) for resumable agent runs
agents/eval Score RunResult values: scorers, suites, pass@1 / latency aggregation
agents/sandbox Sandboxing implementations of the harness.Executor contract (Docker today)
agents/tracing OpenTelemetry instrumentation for agents.Runner
discord Discord-friendly chat sugar: tool-aware Agent, Sender interface, pre-wired media and server-side tool factories
prompt Minimal {name} template substitution for chat messages
runnable Generic Runnable[In, Out] interface and combinators (Pipe, Parallel, Branch, Map, Retry)
workflow Composable steps for multi-step AI pipelines
queue Token-bucket rate limiter for outgoing API calls
memory Conversation and agent memory backends (in-memory, file-based)
cache Prompt-cache helpers for the x-grok-conv-id header
council Multi-agent roundtable: ask N agents the same prompt and synthesize

Documentation

Guide When to read
Getting started First call, picking a model, common 401/404/timeout failure modes.
Configuration Every With* option, request-level fields, env vars, and the headers the SDK sends.
Streaming and tools SSE patterns, tool-use loops, the RunAgent vs agents.Runner decision.
Production guide Retries, rate limits, prompt caching, sandboxing, observability, cost tracking.

Per-package READMEs under chat/, responses/, images/, videos/, voice/, agents/, discord/, and friends document the package-specific surface.

Common configuration

client := grok.New(
    grok.WithAPIKey(os.Getenv("XAI_API_KEY")),
    grok.WithBaseURL("https://api.x.ai"),     // override base URL
    grok.WithHTTPClient(custom),              // bring your own *http.Client
    grok.WithConvID("conv-42"),               // x-grok-conv-id for prompt-cache locality
    grok.WithMaxRetries(3),                   // exponential backoff with jitter on 429/5xx
)

grok.New reads XAI_API_KEY from the environment if WithAPIKey is omitted. Every public method takes a context.Context and respects cancellation.

Examples

The examples/ directory holds runnable demonstrations of every API surface and feature combination, including:

Most examples live in the root module and run with go run ./examples/<name>. A handful that need heavyweight third-party dependencies (notably examples/discord-bot with discordgo) are nested Go modules with their own go.mod; build those from inside their directory so the root module stays slim.

Testing

The repository ships integration tests for every API surface. They read XAI_API_KEY from the environment and skip when absent.

go test ./...                          # unit tests only when XAI_API_KEY is unset
XAI_API_KEY=... go test ./... -v       # full integration sweep

Use grok-4-1-fast-reasoning or grok-4-1-fast-non-reasoning for production-accuracy tests, and grok-3-mini-fast for cheaper smoke tests.

Project

  • Changelog — release-by-release notes following Keep a Changelog.
  • Releasing — versioning policy and the steps to cut a tag.
  • Security policy — coordinated disclosure address, scope, threat model.

References

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:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type APIError

type APIError = apierr.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

func New(opts ...Option) *Client

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
}
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
}

func (*Client) NewGRPC

func (c *Client) NewGRPC(opts ...grpc.DialOption) (*xgrpc.Client, error)

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

func (c *Client) WithConvID(id string) *Client

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
}

type Option

type Option func(*config)

Option configures the Client.

func WithAPIKey

func WithAPIKey(key string) Option

WithAPIKey sets the xAI API key. Defaults to the XAI_API_KEY environment variable.

func WithBaseURL

func WithBaseURL(url string) Option

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

func WithConcurrency(n int) Option

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

func WithConvID(id string) Option

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

func WithHTTPClient(hc *http.Client) Option

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

func WithLogger(l *slog.Logger) Option

WithLogger attaches a slog.Logger that receives request/response events. Successful requests are logged at Info; retries at Warn; failures at Error.

func WithMaxRetries

func WithMaxRetries(n int) Option

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL