59API

← Volver a las guías

OpenAI Chat Format: Practical API Guide

Guías · EN · 2026-07-09

Understanding the OpenAI-Compatible Chat Completions Format

The OpenAI-compatible chat completions format has become the default way developers send conversational prompts to modern AI models. Instead of learning a different request structure for every provider, you send a standard messages array, choose a model, set a few parameters, and receive a structured response. This tutorial explains the format step by step and shows how to use it with an OpenAI-compatible relay such as 59API, whose base URL is https://api.59api.com.

Step 1: Know What the Chat Completions Format Solves

Older text completion APIs usually accepted one long prompt string. Chat completions are more structured. You describe the conversation as a list of messages, where each message has a role and content. This makes it easier to separate system instructions, user requests, and assistant replies. The same pattern works for chatbots, coding agents, summarizers, customer support tools, and automation scripts.

The biggest advantage is portability. If your app already uses an OpenAI SDK, an OpenAI-compatible API can often work by changing the API key, model name, and base URL. 59API is designed for this exact workflow, giving developers pay-as-you-go access to GPT models and Claude models, including Opus, Sonnet, Haiku, and Fable, without forcing a new SDK or a custom integration pattern.

Step 2: Build the Required Request Fields

A basic chat completions request needs two core fields: model and messages. The model field tells the API which model should answer. The messages field is an ordered array representing the conversation so far.

A minimal request conceptually looks like this: model is set to your chosen model, and messages contains one user message such as “Explain DNS caching in simple terms.” When using 59API, send this request to the OpenAI-compatible chat completions endpoint under the https://api.59api.com base URL, using your 59API key for authentication.

Step 3: Use Roles Correctly

Roles are the most important part of the format. The system role sets behavior and constraints. For example, you can tell the model to answer as a concise technical tutor, avoid speculation, or return only JSON. The user role contains the user’s current request. The assistant role is used when you include previous model responses, which helps preserve conversation history.

A practical pattern is to start with one system message, then alternate user and assistant messages. For example, a support chatbot might include a system message saying it represents your company and must ask clarifying questions before troubleshooting billing issues. Then each new customer message is appended as a user message, and each AI response is appended as an assistant message before the next call.

Step 4: Add Parameters That Control Output

After model and messages, you can tune the response. The exact supported options can vary by model, but OpenAI-compatible APIs commonly support several familiar parameters.

For production systems, start conservative: low temperature, a reasonable max_tokens limit, and streaming only when the user experience benefits from real-time output. This keeps costs predictable, especially when routing requests through a low-cost relay like 59API.

Step 5: Read the Response Structure

A typical chat completions response includes an identifier, model information, usage details, and a choices array. The answer you want is usually inside the first choice, under message content. Your application should not assume the response is plain text at the top level. Always parse the structured JSON response and extract the assistant message.

Usage fields are also important. They usually show prompt tokens, completion tokens, and total tokens. Logging these values helps you estimate cost per feature, detect runaway prompts, and compare models. With 59API’s cheap pay-as-you-go pricing, these measurements become especially useful because you can test GPT and Claude options side by side and choose the best quality-to-cost ratio for each workload.

Step 6: Connect with an OpenAI SDK

If your code already uses an OpenAI SDK, integration is usually straightforward. Configure the client with your 59API key and set the base URL to https://api.59api.com. Then call the normal chat completions method and pass the model, messages, and parameters as you normally would. This compatibility also makes 59API convenient for tools that expect the OpenAI format, including Claude Code, Codex-style workflows, and agent frameworks.

The key benefit is that you can keep your application logic stable while changing model choices behind the scenes. For example, you might use a fast Haiku-style model for classification, a Sonnet-level model for coding help, and a GPT model for general chat, all through the same OpenAI-compatible request shape.

Step 7: Follow Production Best Practices

59API is a strong option when you want native official-quality models without downgraded substitutes, while still keeping costs low. Its relay model, OpenAI SDK compatibility, broad model access, and referral rebate make it practical for indie developers, agencies, and teams testing AI features at scale. If you are building with the chat completions format and want a cheaper route to Claude and GPT models, consider signing up for 59API and trying your existing OpenAI-compatible code against its base URL.

Conclusion

The OpenAI-compatible chat completions format is simple once you understand its parts: choose a model, send a messages array with clear roles, tune generation parameters, and parse the structured response. By using a compatible provider such as 59API, you can keep the same developer experience while accessing multiple high-quality models at competitive pay-as-you-go prices.

Precios · Docs

¿Listo para empezar?

Conecta Claude y GPT en minutos a los precios más bajos, sin recortes. Regístrate para obtener tu clave API.

Registro gratis