Log in·Sign up
BestRelay

Back to the Journal

Your Backend Makes AI Access a Product Decision

An AI feature becomes easier to operate when your application owns the boundary between a user action and a model request. Your backend can decide who receives access, constrain spending, and turn provider failures into understandable product behavior. The walkthrough below builds that boundary around a support assistant, but the same sequence works for document analysis, drafting tools, and internal search.

First define the request your application accepts

Start with one narrowly scoped operation: an authenticated customer submits a support question and receives an answer. Decide which inputs the browser may supply, such as the question and a conversation identifier. Keep model selection, system instructions, output limits, and provider credentials under server control.

Define a stable response contract before connecting a model. Your frontend needs an answer, an application request identifier, and a clear completion or failure state. Avoid exposing the upstream response structure as your public contract; that would make future integration changes affect every client.

Keep credentials and authorization inside your backend

Store the relay credential in your deployment platform’s secret storage and make it available only to the server process. The browser should authenticate to your application using its existing session. Your backend then checks whether that user can access the assistant and the requested conversation.

Apply request size limits, per-user quotas, and concurrency controls before making an upstream call. If the assistant uses private documents, authorize access to those documents separately. A valid login does not establish permission to retrieve every customer record or conversation.

Choose the upstream model through server policy

Configure your server’s SDK with BestRelay’s API base and credential. BestRelay supports OpenAI SDK integrations, alongside tools such as Claude Code, Codex, and Gemini CLI. SDK compatibility simplifies transport, but you should still verify that your chosen endpoint supports the message format, streaming behavior, and features your application needs.

For an initial integration, select one model, such as Gemini 2.5 Flash, and keep its documented identifier in server-managed settings. BestRelay forwards each request to the official model named in it without substitution. Its coverage includes Claude, GPT, Gemini, DeepSeek, Kimi, GLM, Qwen, and Grok, so later expansion can remain an explicit application decision.

Assemble context within a deliberate token budget

Build the upstream request from trusted instructions, the authorized conversation history, and the customer’s latest question. Treat retrieved text and user messages as untrusted content. They may contain instructions that attempt to redirect the assistant or expose information.

Reserve space for the answer before adding history. Trim older exchanges or use a carefully maintained summary when the conversation grows. Set a maximum output allowance and a deadline for the upstream request. These limits give your backend a practical basis for controlling latency and potential spend.

Stream answers with explicit completion and failure states

If you stream responses, have your backend translate upstream events into a small application event vocabulary. The client should distinguish incoming text, successful completion, and interruption. A partially displayed answer must remain visibly incomplete if the connection fails.

Propagate client cancellation upstream where supported, and close abandoned connections. Retry selectively: a rejected request may be safe to retry, while an interrupted generation may already have consumed tokens. Automatic retries after output begins can produce duplicate answers and additional charges.

Record provider usage before calculating customer costs

Associate each request with its user, selected model, timing, outcome, and provider-reported usage. BestRelay uses pay-as-you-go billing based on the token counts the provider reports, with cache reads and writes priced separately. Preserve those categories when available instead of treating every input token as equally priced.

Test successful responses, authorization failures, timeouts, cancellation, and quota exhaustion before release. If you need credentials for that trial, create a BestRelay account. Members can also earn referral commission when invited users top up; keep those credits separate from request usage accounting so operating costs remain clear. Launch with a small audience and use observed latency, failures, and spending to adjust your limits.

Want to try the models discussed here? Sign up