Go SDK
For Go projects, the two recommended SDKs are:
- OpenAI compatible:
github.com/openai/openai-go(official) - Anthropic native:
github.com/anthropics/anthropic-sdk-go(official)
Examples are based on the official READMEs pulled via Context7. Specific types and field names should be cross-checked against the upstream repos.
Setup
bash
export SAKRYLLE_API_KEY="sk-xxxxxxxxxxxxxxxx"Go 1.21+ is required.
OpenAI compatible
Install
bash
go get github.com/openai/openai-goMinimal usage
Use option.WithAPIKey and option.WithBaseURL to point requests at Sakrylle:
go
package main
import (
"context"
"fmt"
"os"
"github.com/openai/openai-go/v3"
"github.com/openai/openai-go/v3/option"
)
func main() {
client := openai.NewClient(
option.WithAPIKey(os.Getenv("SAKRYLLE_API_KEY")),
option.WithBaseURL("https://api.sakrylle.com/v1/"),
)
resp, err := client.Chat.Completions.New(context.TODO(), openai.ChatCompletionNewParams{
Model: "gpt-5.6-sol",
Messages: []openai.ChatCompletionMessageParamUnion{
openai.SystemMessage("You are a concise assistant."),
openai.UserMessage("Explain vector databases in one sentence."),
},
})
if err != nil {
panic(err)
}
fmt.Println(resp.Choices[0].Message.Content)
}The trailing slash on
WithBaseURLis the recommended style — it avoids double slashes when the SDK joins paths.
Streaming
go
package main
import (
"context"
"fmt"
"os"
"github.com/openai/openai-go/v3"
"github.com/openai/openai-go/v3/option"
)
func main() {
client := openai.NewClient(
option.WithAPIKey(os.Getenv("SAKRYLLE_API_KEY")),
option.WithBaseURL("https://api.sakrylle.com/v1/"),
)
stream := client.Chat.Completions.NewStreaming(context.TODO(), openai.ChatCompletionNewParams{
Model: "gpt-5.6-sol",
Messages: []openai.ChatCompletionMessageParamUnion{
openai.UserMessage("Count to 5."),
},
})
for stream.Next() {
chunk := stream.Current()
if len(chunk.Choices) > 0 {
fmt.Print(chunk.Choices[0].Delta.Content)
}
}
if err := stream.Err(); err != nil {
panic(err)
}
}Tool calling
go
tools := []openai.ChatCompletionToolUnionParam{
{
OfFunction: &openai.ChatCompletionFunctionToolParam{
Function: openai.FunctionDefinitionParam{
Name: "get_weather",
Description: openai.String("Look up the current weather for a city"),
Parameters: openai.FunctionParameters{
"type": "object",
"properties": map[string]any{
"city": map[string]string{"type": "string"},
},
"required": []string{"city"},
},
},
},
},
}
resp, _ := client.Chat.Completions.New(ctx, openai.ChatCompletionNewParams{
Model: "gpt-5.6-sol",
Messages: []openai.ChatCompletionMessageParamUnion{
openai.UserMessage("What's the weather in Beijing today?"),
},
Tools: tools,
})The SDK evolves quickly — refer to the official README for the current
ChatCompletionToolUnionParam,FunctionDefinitionParam, etc.
Anthropic native
Install
bash
go get github.com/anthropics/anthropic-sdk-goMinimal usage
go
package main
import (
"context"
"fmt"
"os"
"github.com/anthropics/anthropic-sdk-go"
"github.com/anthropics/anthropic-sdk-go/option"
)
func main() {
client := anthropic.NewClient(
option.WithAPIKey(os.Getenv("SAKRYLLE_API_KEY")),
option.WithBaseURL("https://api.sakrylle.com/"),
)
message, err := client.Messages.New(context.TODO(), anthropic.MessageNewParams{
Model: "claude-haiku-4-5-20251001",
MaxTokens: 1024,
Messages: []anthropic.MessageParam{
anthropic.NewUserMessage(anthropic.NewTextBlock("Introduce Claude in one sentence.")),
},
})
if err != nil {
panic(err)
}
fmt.Printf("%+v\n", message.Content)
}Streaming
The standard NewStreaming + message.Accumulate pattern:
go
stream := client.Messages.NewStreaming(context.TODO(), anthropic.MessageNewParams{
Model: "claude-haiku-4-5-20251001",
MaxTokens: 1024,
Messages: []anthropic.MessageParam{
anthropic.NewUserMessage(anthropic.NewTextBlock("Write a four-line poem.")),
},
})
message := anthropic.Message{}
for stream.Next() {
event := stream.Current()
if err := message.Accumulate(event); err != nil {
panic(err)
}
switch e := event.AsAny().(type) {
case anthropic.ContentBlockDeltaEvent:
fmt.Print(e.Delta.Text)
case anthropic.MessageStopEvent:
fmt.Println()
}
}
if err := stream.Err(); err != nil {
panic(err)
}Error handling
Both SDKs expose HTTP status codes and the upstream error body via *Error / *APIError:
go
import (
"errors"
"github.com/openai/openai-go/v3"
)
resp, err := client.Chat.Completions.New(ctx, params)
if err != nil {
var apiErr *openai.Error
if errors.As(err, &apiErr) {
fmt.Println("status:", apiErr.StatusCode)
fmt.Println("body:", apiErr.RawJSON())
}
return err
}For a full error table, see Errors.
