openai-go is OpenAI’s official Go library. It takes a custom base URL with option.WithBaseURL, so it can call any OpenAI-compatible API. BroadNet Inference serves chat completions at https://inference.broadnet.ai/v1. BroadRouter is BroadNet’s own site; this follows the openai-go README for v3.99.0.
Install
go get -u 'github.com/openai/openai-go/v3@v3.99.0'
export BROADNET_API_KEY='your-broadnet-key'
The current release needs Go 1.26 or later.
Chat completion
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.WithBaseURL("https://inference.broadnet.ai/v1"),
option.WithAPIKey(os.Getenv("BROADNET_API_KEY")),
)
resp, err := client.Chat.Completions.New(context.TODO(), openai.ChatCompletionNewParams{
Model: "broadnet/glm-5.3",
Messages: []openai.ChatCompletionMessageParamUnion{
openai.SystemMessage("You are a concise Go reviewer."),
openai.UserMessage("How do I check if a slice is empty in Go?"),
},
MaxTokens: openai.Int(2000), // reasoning counts toward this limit
})
if err != nil {
panic(err)
}
fmt.Println(resp.Choices[0].Message.Content)
}
Run it with go run .. A printed answer is your test: the key, URL and model ID work.
- Model is a plain string.
openai.ChatModelis a type alias forstring, so BroadNet IDs need no conversion. - Use
Chat.Completions. The README leads with the Responses API (client.Responses.New), which BroadNet doesn’t list. Its Chat Completions section says that API is “supported indefinitely”. MaxTokensvsMaxCompletionTokens. The SDK marksmax_tokensas deprecated in favour ofmax_completion_tokens, but BroadNet’s API reference describesmax_tokens. UseMaxTokens.SystemMessage, notDeveloperMessage. The README example usesDeveloperMessage, which sends thedeveloperrole. BroadNet’s API reference doesn’t mention that role, so usesystemanduser.
Streaming with usage
stream := client.Chat.Completions.NewStreaming(context.TODO(), openai.ChatCompletionNewParams{
Model: "broadnet/deepseek-v4.1-flash",
Messages: []openai.ChatCompletionMessageParamUnion{openai.UserMessage("Count from one to five.")},
MaxTokens: openai.Int(2000),
StreamOptions: openai.ChatCompletionStreamOptionsParam{
IncludeUsage: openai.Bool(true),
},
})
defer stream.Close()
for stream.Next() {
chunk := stream.Current()
if len(chunk.Choices) > 0 {
fmt.Print(chunk.Choices[0].Delta.Content)
}
if chunk.Usage.TotalTokens > 0 {
fmt.Printf("\n%d tokens\n", chunk.Usage.TotalTokens)
}
}
if err := stream.Err(); err != nil {
panic(err)
}
BroadNet’s docs say usage arrives in the last chunk when stream_options.include_usage is true; that chunk has no choices, hence the length check. See streaming chat completions for the wire format.
Errors and retries
The SDK returns *openai.Error for non-success statuses. Use errors.As and read StatusCode:
var apierr *openai.Error
if errors.As(err, &apierr) && apierr.StatusCode == 429 {
// back off, then retry
}
openai-go retries some errors twice by default with a short exponential backoff. Change it with option.WithMaxRetries(n). Retries count toward BroadNet’s rate limits.
Troubleshooting
- 401. The key is wrong, expired or out of allowance. If you passed no
WithAPIKey, the SDK usedOPENAI_API_KEY. - 404. The base URL must end in
/v1, and the call must go toChat.Completions, notResponses. Then check the model ID. Paid models need a top-up; the $5 credit covers onlybroadnet/gemma-4-26b-a4band the two Transcribe models. - Empty
Content. Reasoning used the output budget. RaiseMaxTokens. Why answers come back empty. - 429. A pay-as-you-go key allows 30 requests a minute and 4 in flight. Limit goroutines that call the API at once. See errors and retries.
