ai-sdk
Grafana AI SDK for Go — streaming, tool-calling AI backends that speak fluent @ai-sdk/react
What is it?
What it is
A Go SDK for calling language models, streaming responses, executing tools, and serving AI-powered endpoints.
Why it exists
To provide a unified API for model calls, streaming, and multi-step agents in Go that is wire-compatible with Vercel's AI SDK React frontend hooks.
Who should use it
Go developers building AI-powered applicationsBackend engineers needing to integrate LLMs with React frontendsDevelopers using Vercel AI SDK React hooks who want a Go backendEngineers requiring structured output and tool-calling in GoSystems architects implementing agentic workflows in Go
Who should avoid it
Frontend developers looking for a pure TypeScript/JavaScript solutionDevelopers working in languages other than Go
How it works
A quick walkthrough in plain English
How ai-sdk works
Step 1 of 3
You interact with it
Open ai-sdk, send a request, or connect it to your stack.
Features
Advantages
- Unified API for model calls, streaming, tools, and structured output across providers
- Seamless integration with existing @ai-sdk/react frontends via SSE
- Built-in retry logic and multi-step tool execution
- Extensible tool system with approval for consequential actions
- Strong observability and metrics for production use
- Open-source with Apache-2.0 license and active community
Disadvantages
- Limited to Go language; no native support for other backend languages
- Requires knowledge of Go and SSE for full stack integration
- Provider support is currently limited to a few major APIs
- Learning curve for configuring providers and middleware
- Documentation is evolving; some advanced features may lack examples
Installation
native
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
FAQ
How do I install the AI SDK and add a provider in my Go project?
Create a Go module, then run `go get github.com/grafana/ai-sdk` to install the core SDK and `go get github.com/grafana/ai-sdk/providers/anthropic` (or another provider) to add support for that model provider. The README’s "Install" section shows the exact commands and explains that you’ll need an API key as an environment variable for the chosen provider.
How can I generate text from a model in a simple Go program?
Use `aisdk.GenerateText` with a provider instance. Example from the README: ```go model := anthropic.New(os.Getenv("ANTHROPIC_API_KEY"), "claude-sonnet-5") result, err := aisdk.GenerateText(ctx, model, aisdk.WithModelMessages(provider.UserText("Explain goroutines in one sentence."))) ``` The result’s `Text` field contains the model’s response.
How do I stream a model response from Go to a React frontend?
The SDK exposes `aisdk.StreamText` which writes Server‑Sent Events (SSE) that a React hook like `useChat` can consume. The README’s architecture diagram shows the flow: Go backend → SSE → React frontend. Follow the "Build a full‑stack chat" guide for the full setup.
How can I get typed, structured data from a model?
Use the SDK’s structured output feature to request schema‑validated objects, arrays, or choices. The "Structured output" guide explains how to define a schema and retrieve typed data from the model’s response.
How do I enable a model to call Go functions (tool calling)?
Define composable tools in Go and register them with the SDK. The model can then invoke these tools, and you can require approval for consequential actions. The "Tools" guide provides step‑by‑step instructions.
Which AI providers are supported and how do I add a new one?
The SDK supports Anthropic, Amazon Bedrock, OpenAI, OpenAI‑compatible APIs, and Grafana’s hosted endpoint. To add a provider, install its Go package (e.g., `go get github.com/grafana/ai-sdk/providers/openai`) and follow the "Choose a provider" section for configuration details.
Featured in Videos
YouTube tutorials and walkthroughs for ai-sdk
Alternatives
Similar projects ranked by category, topics, and text overlap.