Log in·Sign up
BestRelay

Back to the Journal

Reliable Claude Integrations Start With Explicit Message State

Direct Messages API access makes application responsibilities explicit

A successful Anthropic integration starts with a simple distinction: the Messages API generates a response, but your application manages the conversation. Calling it directly exposes the request structure, response blocks, stop reasons, and usage data that frameworks sometimes hide. This walkthrough builds from one text exchange toward a production-ready interaction without assuming that a successful first response proves the integration is complete.

First establish credentials and the intended connection

Create an Anthropic account, obtain an API key through its console, and confirm that billing and model access are available. Keep the key on your server, outside browser bundles and application logs. For direct HTTP requests, Anthropic requires authentication, a supported API version header, and the appropriate content type.

This article is published by BestRelay, a quality-first API relay. Distinguish a direct Anthropic connection from a relayed connection before configuring authentication or request routing. BestRelay forwards each request to the official model named in it without substitution. A relay address does not automatically establish support for every provider-native request format; confirm the supported route and authentication requirements first.

Next define a minimal request with clear boundaries

Choose a model identifier documented for the account and connection you are using. Claude Haiku 4.5 is one model offered through BestRelay, but a display name should not be assumed to be the exact identifier accepted in an API request.

A basic Messages request includes the model, an output token limit, and a messages array containing a user message. Put application-level instructions in the separate, top-level system parameter rather than inventing a system role inside that array. Keep your first prompt narrow enough that you can recognize a correct response immediately.

The output token limit is a ceiling, not a promised response length. Select it for the task, leaving room for a complete answer while avoiding an unnecessarily large allowance.

Then inspect content blocks before displaying the answer

A Messages response contains structured content rather than one guaranteed text string. For a simple exchange, extract text blocks for display, but preserve the full response structure for subsequent processing. Responses can also contain tool-related blocks, so treating every block as printable text creates fragile behavior.

Inspect the stop reason alongside the content. A normal turn ending differs from reaching the output limit or requesting a tool. If generation reaches its limit, do not silently present the result as complete. Record the model, usage, and request identifier when available, while excluding secrets and sensitive prompt content from routine logs.

Continue conversations by preserving the message sequence

The standard Messages API does not maintain your application's conversation history between requests. To continue an exchange, send the relevant earlier user and assistant messages along with the new user message. Preserve assistant content blocks accurately rather than rebuilding history from visible text alone.

As history grows, establish a context policy. Retain essential instructions and recent turns, and summarize or remove older material deliberately. Token counting can help estimate input size before submission, but successful validation does not guarantee that every later request will fit after new content is added.

Add tools and streaming as explicit state transitions

When enabling tools, provide clear tool descriptions and input schemas. A tool request is not permission to execute arbitrary operations. Validate arguments, enforce authorization, run the approved operation, and return its result in a user message associated with the matching tool-use identifier. Then request the assistant's continuation.

For streaming, process the documented event sequence and assemble content incrementally. Do not mistake the first fragment for a finished response. Handle interrupted connections, partial output, and terminal events explicitly. Apply bounded retries with backoff for transient failures, recognizing that retrying after a partial response can duplicate work or charges.

Measure usage before expanding the integration surface

Check reported input and output usage after each request, including cache-related fields when applicable. Cache reads and writes have distinct accounting implications; repeated prompts alone do not prove that caching occurred.

BestRelay uses pay-as-you-go billing based on provider-reported token counts, with cache reads and writes priced separately. It covers Claude, GPT, Gemini, and other model families, and supports Claude Code, Codex, Gemini CLI, and OpenAI SDKs. That broader compatibility does not make their request formats interchangeable. Members also earn referral commissions when invited users top up. Create a BestRelay account when you are ready to test a relay connection.

Want to try the models discussed here? Sign up