BroadRouter
Get started

Call BroadNet Inference from Go with the openai-go SDK

Updated

Install github.com/openai/openai-go/v3 and create the client with option.WithBaseURL("https://inference.broadnet.ai/v1") and option.WithAPIKey set to your BroadNet key. Call client.Chat.Completions.New with a BroadNet model ID such as broadnet/glm-5.3 as a plain string. Use chat completions, not the Responses API, and set MaxTokens with room for reasoning.

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.ChatModel is a type alias for string, 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”.
  • MaxTokens vs MaxCompletionTokens. The SDK marks max_tokens as deprecated in favour of max_completion_tokens, but BroadNet’s API reference describes max_tokens. Use MaxTokens.
  • SystemMessage, not DeveloperMessage. The README example uses DeveloperMessage, which sends the developer role. BroadNet’s API reference doesn’t mention that role, so use system and user.

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 used OPENAI_API_KEY.
  • 404. The base URL must end in /v1, and the call must go to Chat.Completions, not Responses. Then check the model ID. Paid models need a top-up; the $5 credit covers only broadnet/gemma-4-26b-a4b and the two Transcribe models.
  • Empty Content. Reasoning used the output budget. Raise MaxTokens. 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.

Questions

Can I set the BroadNet URL with environment variables instead of options?

Yes. openai-go reads OPENAI_API_KEY and OPENAI_BASE_URL by default, so openai.NewClient() with no options works if both hold BroadNet values. An explicit option.WithBaseURL overrides OPENAI_BASE_URL.

How do I pass a BroadNet model ID when Model expects openai.ChatModel?

ChatModel is a type alias for string in openai-go, so Model "broadnet/glm-5.3" compiles as is.

Why use SystemMessage rather than DeveloperMessage?

DeveloperMessage sends the developer role, which OpenAI introduced for its own models. BroadNet's API reference doesn't mention that role, so the standard system role is the safer choice.

Which Go version do I need?

The current openai-go v3 needs Go 1.26 or later. Its README says to pin v3.74.0 if you must stay on Go 1.25.

Read as MarkdownAsk ChatGPTAsk ClaudeAsk Perplexity