The Gopher talks into a tin can whose string arcs up to the Ghost, a stone android, small on a high terrace with the other can to its head, while a long blank receipt curls out of the Gopher's can across the floor.

Basics

Calling a model’s API from Go with net/http

A chat completions client in standard-library Go, with deadlines, error checks, and finish reasons.

Updated Beginner

On the agent compass Stage 2 of 4 · Call the model

To a Go program, a model is a web service: you POST some JSON and decode the JSON that comes back. net/http and encoding/json/v2 cover all of it. The big providers do publish official Go SDKs (software development kits: libraries that wrap an API in ready-made Go types and functions; see the glossary), which bring typed options, built-in retries, and streaming helpers. The catch for an agent is that an SDK makes some choices on your behalf that an agent should make on purpose, such as the timeout for a call, whether a failure gets retried, and what happens to a reply that ended before it was done. Building the client yourself once puts each of those choices in front of you; a later project can switch to an SDK where one is useful.

The client shown here comes from the companion code, and we’ll follow one call from request to decoded reply.

One endpoint

Every request the client sends goes here:

output
POST https://openrouter.ai/api/v1/chat/completions

OpenRouter is a single front door to many model providers: one API, one key, and one invoice, with the model named in the request and OpenRouter passing it to a provider that hosts it. The request format is OpenAI’s chat completions format, which has become the common one: most other hosted services and local model servers accept it as well. That makes the base URL just another setting. Change it to a local server’s address, for example Ollama’s http://localhost:11434/v1, and the client works against that server unchanged.

The request body names the model and carries the conversation. The response carries the model’s message plus metadata: the reason it stopped writing and what the call used.

Wire types, kept apart

The JSON the API speaks lives in wire types: Go structs that match the JSON sent over the network, field for field. They are unexported (their names start with a lowercase letter, so only the client package can use them) and separate from the agent’s own Message:

ch02/openrouter/wire.go
type chatRequest struct {
	Model     string        `json:"model"`
	Messages  []chatMessage `json:"messages"`
	MaxTokens int           `json:"max_tokens,omitzero"`
}

type chatMessage struct {
	Role    string `json:"role"`
	Content string `json:"content"`
}

Separate types let each side evolve on its own schedule. The API has its own field names and extra fields an agent never reads, and the agent’s types are shaped around the agent. With the translation in one file, the rest of the program never learns the API’s conventions, and swapping in an API with another shape later means writing another translation file.

Each field carries a json tag, a short string after the field that names its JSON key (struct tags). encoding/json/v2, which Go 1.27 ships in the standard library, compares names case-sensitively, so every field needs one.

Why MaxTokens is omitzero. MaxTokens is marked omitzero rather than omitempty. Under v2, omitempty drops a field only when its encoded JSON is empty, like "" or [], and the number 0 doesn’t count as empty. With omitempty, every request would include "max_tokens": 0, asking the model for a reply no longer than zero tokens.

The call

The client’s zero values are useful: leave the base URL empty and it uses OpenRouter; leave the *http.Client nil (you didn’t supply one) and it uses http.DefaultClient, the ready-made client in net/http; leave the timeout at zero and it allows two minutes. Generate is the single method that makes the client a Model the agent loop can use:

ch02/openrouter/client.go
// Generate sends the conversation to the model and returns its reply.
func (c *Client) Generate(ctx context.Context,
	req agent.Request) (agent.Reply, error) {
	ctx, cancel := context.WithTimeoutCause(ctx, c.timeout(),
		ErrCallTimeout)
	defer cancel()

	body, err := json.Marshal(c.wireRequest(req))
	if err != nil {
		return agent.Reply{}, fmt.Errorf("encode request: %w", err)
	}
	httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost,
		c.baseURL()+"/chat/completions", bytes.NewReader(body))
	if err != nil {
		return agent.Reply{}, fmt.Errorf("build request: %w", err)
	}
	httpReq.Header.Set("Authorization", "Bearer "+c.APIKey)
	httpReq.Header.Set("Content-Type", "application/json")

	resp, err := c.httpClient().Do(httpReq)
	if err != nil {
		return agent.Reply{}, fmt.Errorf("send request: %w",
			callError(ctx, err))
	}
	defer resp.Body.Close()
	return readReply(ctx, resp)
}

Everything else is standard net/http. NewRequestWithContext binds the request to ctx, so a cancelled run abandons the call, including while the body is still arriving. The API key travels in the Authorization header as Bearer <key>, called a bearer token because whoever holds it can make calls on your account. defer resp.Body.Close() gives the connection back no matter how the function returns.

A deadline with a reason

The default http.Client will wait forever, and a healthy model call can take over a minute, so the client needs some limit. http.Client.Timeout is the built-in setting for one, but it applies to every request through that client, and clients are usually shared by much of a program. It also gives no way to say which limit expired.

The client instead sets a deadline on the context for each call. The takeaway: a slow model can’t hang the program, because every call has a time limit, and the error that comes back says why the call stopped.

How the deadline works. The client derives the deadline from ctx for each call. That deadline belongs to one call only; it stacks with any deadline the caller already set (the sooner of the two fires); and it can carry a reason. context.WithTimeoutCause attaches ErrCallTimeout to the cancellation, and context.Cause retrieves it. A caller can then test the error two ways: errors.Is(err, context.DeadlineExceeded) says a deadline fired, and ErrCallTimeout says it was this client’s. net/http doesn’t consistently preserve both, and which one survives depends on whether the connection is HTTP/1.1 or HTTP/2. So the client wraps in whatever is missing using %w: %w, which wraps two errors in one, and its tests verify the result over both protocol versions.

Where two minutes comes from. The two-minute figure is measured, from the recorded runs in the companion code:

Measurement Result
Longest single call 76 seconds (z-ai/glm-5.3-flash, producing 2,880 reasoning tokens)
Calls that returned in under three seconds, in the bug-fix runs 90%

Two minutes covers the slow reasoning case and still catches a call that is stuck. The two official Go SDKs pick very different defaults: openai-go has no timeout unless you set one, and anthropic-sdk-go allows a non-streaming call ten minutes.

A sequence diagram: the chat loop calls Generate with its ctx; Generate derives a ctx with a two-minute deadline whose cause is ErrCallTimeout and sends the POST through net/http to OpenRouter, which forwards it to a provider; the reply comes back with usage and cost. If Ctrl-C or the deadline fires first, the request is abandoned and the error carries the kind and the cause.A sequence diagram: the chat loop calls Generate with its ctx; Generate derives a ctx with a two-minute deadline whose cause is ErrCallTimeout and sends the POST through net/http to OpenRouter, which forwards it to a provider; the reply comes back with usage and cost. If Ctrl-C or the deadline fires first, the request is abandoned and the error carries the kind and the cause.A sequence diagram: the chat loop calls Generate with its ctx; Generate derives a ctx with a two-minute deadline whose cause is ErrCallTimeout and sends the POST through net/http to OpenRouter, which forwards it to a provider; the reply comes back with usage and cost. If Ctrl-C or the deadline fires first, the request is abandoned and the error carries the kind and the cause.A sequence diagram: the chat loop calls Generate with its ctx; Generate derives a ctx with a two-minute deadline whose cause is ErrCallTimeout and sends the POST through net/http to OpenRouter, which forwards it to a provider; the reply comes back with usage and cost. If Ctrl-C or the deadline fires first, the request is abandoned and the error carries the kind and the cause.A sequence diagram: the chat loop calls Generate with its ctx; Generate derives a ctx with a two-minute deadline whose cause is ErrCallTimeout and sends the POST through net/http to OpenRouter, which forwards it to a provider; the reply comes back with usage and cost. If Ctrl-C or the deadline fires first, the request is abandoned and the error carries the kind and the cause.
One call’s life: the run’s context and the call’s own deadline both reach the HTTP request, and whichever ends first cancels it.

Reading the reply

There are four distinct ways a call can fail, and the client looks for each one:

  • A status other than 200. The API turned the request down, for reasons like an invalid key (401), an empty account (402), a request bigger than the model accepts, or a provider outage. The returned error includes the status and the message the API sent.
  • An error object inside a 200. OpenRouter took the request, but the provider broke down before it produced a reply.
  • A finish reason of error. The provider started replying and then broke down.
  • A 200 whose body isn’t the expected JSON. A proxy’s HTML error page or a body cut off mid-transfer fails to decode, and the client returns a decode error.

The client reads the body through io.LimitReader with a 16 MB ceiling, so a misbehaving server can’t keep it reading without end.

Once the reply checks out, the client copies the parts the agent uses: the message, the usage (input and output tokens, cached and reasoning tokens, and the dollar cost OpenRouter includes in every reply), and the finish reason:

Value Meaning
stop The model finished its reply
length It reached the output limit; the text is cut off
tool_calls It is asking for tools
content_filter A safety filter blocked or cut the reply
error The provider failed partway through

A length reply is returned normally, with the reason in a field. The text is truncated, but it is still text, and only the caller knows whether to keep it, ask again with a larger limit, or stop. The agent loop built on top should be stricter than the client and refuse to treat a truncated reply as a finished answer.

What it deliberately leaves out

Each call gets exactly one attempt. Failures are routine with model APIs: 429 rate limits, 502 and 503 from overloaded providers, connections that drop halfway through a reply. The official Go SDKs handle this by retrying twice by default. That’s handy, but the caller can’t see it happen: one call just looks slow, and any budget that counts calls misses the extra attempts. Put retries in the agent loop instead, so every attempt is visible in the logs, counts against the budget, and stops when the run is cancelled.

Run it

From the code directory of a clone of the companion repository:

terminal
$ go run ./ch02

If OPENROUTER_API_KEY is unset, the program plays back a three-turn chat recorded from the real API and prints the cost of each call. The client sends its requests through an HTTP transport, the part of net/http that actually carries a request; here the program swaps in a transport that reads the saved responses from files instead of using the network, and to the client they look exactly like real replies. The start of that replay looks like this:

go run ./ch02, with no key
No OPENROUTER_API_KEY: replaying a saved conversation.
> What is a goroutine?
A goroutine is a lightweight thread of execution managed by the Go
runtime rather than the operating system. You start one by placing the
`go` keyword before a function call, such as `go doSomething()`, and
it runs concurrently with the rest of your program. Goroutines are
cheap to create (they start with a small, growable stack), so a Go
program can run thousands or even millions of them at once.
[43 tokens in, 122 out, $0.000065; total $0.000065]

If the key is set, you get a live chat with the default model. For a local server, set OPENROUTER_BASE_URL and OPENROUTER_MODEL, and give OPENROUTER_API_KEY any non-empty value, because an empty key means replay.