Build streaming AI backends in Go with React compatibility
Go SDK for model calls, streaming, tools, structured output and agents, wire-compatible with the Vercel AI SDK React hooks.
0.1.0-alpha.1Add to Favorites
Why it matters
Developers hire this SDK to build production-ready AI-powered backends in Go that can call language models, stream responses to React frontends, execute tools, and generate structured output across multiple providers without writing protocol adapters.
Outcomes
What it gets done
Stream model responses from Go endpoints directly to React useChat and useCompletion hooks via SSE
Execute multi-step tool calls where models invoke Go functions with approval gates for sensitive actions
Generate schema-validated structured output as typed objects, arrays, and choices from any supported provider
Switch between Anthropic, OpenAI, Bedrock, and Grafana providers with unified API and production observability
Source
Get it from source
Spark does not host a copy of it.
Open sourceReports
Agent outcome reports
No reports yet
Overview
A Go SDK for calling language models, streaming responses, running tools and serving AI endpoints. It follows Vercel's AI SDK design and stays wire-compatible with its React hooks. Use it when you build model calls, streaming chat, tools or agents in Go, or want a Go backend for an AI SDK React frontend.
What it does
Grafana AI SDK for Go lets you call language models, stream responses, execute tools and serve AI-powered endpoints from Go. You can use it on its own or pair it with an AI SDK React frontend. It gives Go applications one API for model calls, streaming, tools, structured output and multi-step agents across supported providers. It follows the design of Vercel's AI SDK and stays wire-compatible with its TypeScript frontend hooks, so a Go endpoint can stream Server-Sent Events directly to hooks such as useChat. That means you can reuse an existing AI SDK React frontend, or replace a TypeScript backend with Go, without a protocol adapter. On the Go side, aisdk.StreamText produces the stream and aisdk.WriteUIMessageStream writes it to the response over SSE.
Features:
StreamTextandGenerateTextstream a response or wait for the complete result, with retries and multi-step tool execution.- React compatibility serves
useChat,useCompletionanduseObject. - Composable tools call plain Go functions from a model and can require approval for consequential actions.
- Structured output generates schema-validated objects, arrays and choices.
- Multiple providers: Anthropic, Amazon Bedrock, OpenAI and OpenAI-compatible APIs.
- Production controls: timeouts, fallback, logging, Prometheus metrics and Agent Observability.
When to use - and when NOT to
Use it when you want to build model calls, streaming chat, tool calling, typed outputs or agent loops in Go, especially behind an AI SDK React chat client. It is not a frontend library, so a React client needs the separate AI SDK hooks. The repository has two licenses: the reusable SDK is Apache-2.0, and the separate ai-gateway/ module is AGPL-3.0-only, so check which one you depend on.
Inputs and outputs
Inputs are a model from a provider package plus messages and options. In the quick start, anthropic.New takes an API key from the ANTHROPIC_API_KEY environment variable and a model name, and aisdk.GenerateText is called with a context, the model and a user text message built with provider.UserText. The program exits with an error if the key is not set. The output is a result whose text field is printed, or a stream for a client, or typed structured output.
How to install
Create a Go project, then install the core module and one provider:
go get github.com/grafana/ai-sdk
go get github.com/grafana/ai-sdk/providers/anthropic
Save a main.go that makes one model call and prints the response, then run it with an Anthropic API key. Other providers are documented in the provider overview. To stream the response to a React client, follow the full-stack chat guide.
Who it's for
Go developers building AI backends who want streaming, tool calling and agent loops with a familiar AI SDK protocol, and teams that want to keep a React chat frontend while moving the backend to Go. Contributions follow upstream parity with the Vercel AI SDK and spec-driven development with OpenSpec.
Source README
Call language models, stream responses, execute tools, and serve AI-powered endpoints from Go. Use the SDK on its own or pair it with an AI SDK React frontend.
Quick start · Documentation · Examples · API reference
Why
The SDK gives Go applications one API for model calls, streaming, tools,
structured output, and multi-step agents across supported providers. It follows
the design of Vercel's AI SDK and stays wire-compatible
with its TypeScript frontend hooks. A Go endpoint can stream Server-Sent Events
(SSE) directly to hooks such as useChat.
Go backend React frontend
────────── ──────────────
aisdk.StreamText(...) ── SSE ──▶ useChat({ transport })
aisdk.WriteUIMessageStream(w, …) // same protocol
See How a request runs for the generation, tool, and streaming flow. Reuse an existing AI SDK React frontend or replace a TypeScript backend with Go without adding a protocol adapter.
Features
StreamText/GenerateText- stream a response or wait for the complete result, with retries and multi-step tool execution- React compatibility - serve
useChat,useCompletion, anduseObject - Composable tools - call plain Go functions from a model and require approval for consequential actions
- Structured output - generate schema-validated objects, arrays, and choices
- Multiple providers - call Anthropic, Amazon Bedrock, OpenAI, and OpenAI-compatible APIs
- Production controls - configure timeouts, fallback, logging, Prometheus metrics, and Agent Observability
Install
Create a Go project and install the core module and one provider:
mkdir ai-sdk-quickstart
cd ai-sdk-quickstart
go mod init example.com/ai-sdk-quickstart
go get github.com/grafana/ai-sdk
go get github.com/grafana/ai-sdk/providers/anthropic
See Choose a provider for Amazon Bedrock, OpenAI, and OpenAI-compatible APIs.
Quick start
Save this complete program as main.go. It makes one model call and prints the
response:
package main
import (
"context"
"fmt"
"log"
"os"
aisdk "github.com/grafana/ai-sdk"
"github.com/grafana/ai-sdk/provider"
"github.com/grafana/ai-sdk/providers/anthropic"
)
func main() {
apiKey := os.Getenv("ANTHROPIC_API_KEY")
if apiKey == "" {
log.Fatal("ANTHROPIC_API_KEY is required")
}
model := anthropic.New(apiKey, "claude-sonnet-5")
result, err := aisdk.GenerateText(context.Background(), model,
aisdk.WithModelMessages(provider.UserText("Explain goroutines in one sentence.")),
)
if err != nil {
log.Fatal(err)
}
fmt.Println(result.Text)
}
Run it with an Anthropic API key:
ANTHROPIC_API_KEY=sk-... go run .
For project initialization and credential guidance, follow Installation. To stream this response to a React client, continue with Build a full-stack chat.
Where to go next
| Goal | Start here |
|---|---|
| Make model calls from Go | Generate text from Go |
| Build a React chat | Full-stack chat |
| Return typed data | Structured output |
| Let a model call Go code | Tools |
| Build a reusable agent | Agent loops |
| Choose a model provider | Provider overview |
| Add logging or observability | Middleware overview |
| Prepare for production | Production checklist |
Full index: Documentation · Runnable code: Examples · Exact APIs: pkg.go.dev
FAQ
Common questions
Discussion
Questions & comments · 0
Sign In Sign in to leave a comment.