---
title: "openai-go with a custom base URL (WithBaseURL) | BroadRouter"
description: "Use the official openai-go v3 SDK with BroadNet Inference through option.WithBaseURL, with chat completions, streaming with usage and error handling in Go."
url: https://broadrouter.com/guides/openai-go-sdk/
lang: en
updated: 2026-11-16
publisher: BroadRouter (a BroadNet site); API by BroadNet Inference, https://inference.broadnet.ai
---

# Call BroadNet Inference from Go with the openai-go SDK

Updated 2026-11-16

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

```bash
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

```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.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

```go
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](https://broadrouter.com/guides/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`:

```go
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](https://broadrouter.com/guides/reasoning-tokens-empty-response/).
-   **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](https://broadrouter.com/guides/errors-and-retries/).

## Terms in this guide

[Chat Completions API](https://broadrouter.com/glossary/chat-completions-api/)

The Chat Completions API is OpenAI's HTTP endpoint, POST /v1/chat/completions, that takes a model name and a list of role-tagged messages and returns the model's next message. Its request and response shape became the common format that OpenAI-compatible providers and SDKs reuse.

[Pay-as-you-go](https://broadrouter.com/glossary/pay-as-you-go/)

Pay-as-you-go is a pricing model in which you pay only for what you use, metered per unit such as tokens, requests or audio minutes, with no subscription or minimum commitment. Most LLM APIs bill this way, from a prepaid balance or a periodic invoice.

[Base URL](https://broadrouter.com/glossary/base-url/)

A base URL is the fixed root address of an API, such as https://api.example.com/v1, that a client puts in front of every endpoint path, such as /chat/completions. In OpenAI-compatible SDKs, changing the base URL, together with the API key and model ID, points the same code at a different provider.

[Rate limit](https://broadrouter.com/glossary/rate-limit/)

A rate limit is a cap an API places on how much a client can use it in a time window, such as requests per minute or tokens per minute. Requests over the limit are refused, usually with HTTP 429 Too Many Requests, until usage falls back under it.

[Streaming](https://broadrouter.com/glossary/streaming/)

Streaming is a way of returning a language model response in small chunks while it is still being generated, instead of as one reply at the end. OpenAI-compatible APIs stream over server-sent events when the request sets stream to true.

[Concurrency](https://broadrouter.com/glossary/concurrency/)

Concurrency is the number of requests a client has in flight at the same time. LLM APIs often cap it per key or plan, separately from requests per minute, because each open request holds GPU capacity until its response has finished.

[Exponential backoff](https://broadrouter.com/glossary/exponential-backoff/)

Exponential backoff is a retry strategy in which a client waits longer after each failed attempt, typically doubling the delay up to a cap, before trying again. Adding random jitter to each wait spreads out retries from many clients so they do not hit the server at the same moment.

[OpenAI-compatible API](https://broadrouter.com/glossary/openai-compatible-api/)

An OpenAI-compatible API is an HTTP API that accepts the same request and response formats as the OpenAI API, such as POST /v1/chat/completions, so OpenAI SDKs and tools can use it by changing only the base URL, the API key and the model name.

## 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.

## Related guides

-   [Use the OpenAI Python SDK with BroadNet Inference](https://broadrouter.com/guides/openai-python-sdk/)
-   [Use the OpenAI Node.js SDK with BroadNet Inference](https://broadrouter.com/guides/openai-node-sdk/)
-   [Stream chat completions and handle dropped connections](https://broadrouter.com/guides/streaming-chat-completions/)
-   [BroadNet Inference error codes and when to retry](https://broadrouter.com/guides/errors-and-retries/)
-   [All tools that work with BroadNet Inference](https://broadrouter.com/integrations/): SDKs, coding agents, chat UIs and automation tools, with exact settings for each

Try it on your real workload. New accounts start with $5 of credit for Gemma and transcription. DeepSeek, GLM and Qwen3.8 need a top-up.

[Get started](https://inference.broadnet.ai/account/?utm_source=broadrouter&utm_medium=referral&utm_campaign=guides-openai-go-sdk&utm_content=page-cta) ·

**Sources**

-   [openai-go README (v3.99.0, Go 1.26+) (checked 2026-11-15)](https://github.com/openai/openai-go)
-   [openai-go source, ChatCompletionNewParams and option.WithBaseURL (checked 2026-11-15)](https://github.com/openai/openai-go/blob/main/chatcompletion.go)
-   [BroadNet Inference API reference (checked 2026-11-15)](https://inference.broadnet.ai/docs?utm_source=broadrouter&utm_medium=referral&utm_campaign=guides-openai-go-sdk&utm_content=sources)
