Build streaming AI backends in Go with React compatibility
Go SDK for streaming, tool-calling LLM backends that speak the same protocol as Vercel's 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
Ai Sdk
Grafana AI SDK is a Go library for calling LLMs, streaming responses, executing tools, and generating structured output, wire-compatible with Vercel's AI SDK React hooks like useChat and useObject. Use it to build a Go backend for an AI SDK React frontend, or to replace a TypeScript AI backend, when streaming, tool-calling, and structured output need to work over the standard AI SDK protocol.
What it does
Grafana AI 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, so a Go endpoint can stream Server-Sent Events directly to hooks such as useChat without a protocol adapter. StreamText and GenerateText stream a response or wait for the complete result, with retries and multi-step tool execution; the SDK serves useChat, useCompletion, and useObject for React compatibility; tools are composable plain Go functions a model can call, with a required-approval path for consequential actions; and structured output generates schema-validated objects, arrays, and choices.
When to use - and when NOT to
Use it to build an AI-powered backend in Go that either pairs with an existing AI SDK React frontend or replaces a TypeScript backend outright, when you want streaming, tool-calling, and structured output without hand-rolling the SSE protocol that useChat expects. It fits production services that need timeouts, fallback, logging, Prometheus metrics, and Agent Observability middleware built in. It is specific to Go backends speaking the AI SDK wire protocol, so it is not the right choice if your frontend or backend stack has no AI SDK React compatibility requirement and a provider's own native SDK would be simpler.
Inputs and outputs
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
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 ANTHROPIC_API_KEY=sk-... go run .. Input is a sequence of model messages built with helpers like provider.UserText; output is generated text, a stream of SSE events for a React client, or a schema-validated structured object depending on which function is called.
Integrations
Supported providers include Anthropic, Amazon Bedrock, OpenAI, OpenAI-compatible APIs, and Grafana's own hosted endpoint for internal services. It integrates with an AI SDK React frontend via the same wire protocol used by useChat, useCompletion, and useObject, and ships production middleware for timeouts, fallback, logging, and Prometheus metrics, plus an Agent Observability middleware. Licensed under Apache License 2.0, following the design of Vercel's AI SDK, itself also Apache-2.0 licensed.
Who it's for
Go backend developers who want to call LLMs, stream responses, and expose tool-calling or structured-output endpoints that a React frontend can consume with the standard AI SDK hooks, without writing their own streaming protocol layer.
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.
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.