Skip to main content
GatelitClient routes chat requests to any model through your Gatelit gateway. The gateway handles provider routing, authentication, policy enforcement, spend tracking, and logging. Your client only needs a model name and messages.

Constructor

string
required
Base URL of your deployed Gatelit gateway. For example: "https://gateway.yourapp.com".
() => Promise<{ token: string; scheme: TokenScheme }>
required
Called before each request to retrieve a fresh auth token. Return an object with a token string and a scheme value.Cache the token internally and only re-fetch when it is close to expiry to avoid unnecessary round-trips.scheme accepts one of three values:

Example


Methods

chat(options)

Sends a chat request and returns the full response once the model finishes.

Options

string
required
Model in "provider/model-name" format. For example: "openai/gpt-4o" or "anthropic/claude-3-5-sonnet-20241022".
ChatMessage[]
required
Array of messages in the conversation. Each message has a role ("system", "user", or "assistant") and a content string.
number
Maximum number of tokens to generate in the response.
number
Sampling temperature. Higher values produce more varied output.
object
Structured output configuration. Use { mode: "json_object" } to instruct the model to return valid JSON of any shape, or { mode: "json_schema", ... } to constrain the output to a specific schema.
string
Links this request to a saved prompt in the dashboard for log correlation.
AbortSignal
An AbortSignal to cancel the in-flight request.

Returns: ChatResponse

string
required
The assistant’s response text.
string
required
Resolved model identifier in "provider/model-name" format. May differ from the requested model if a fallback was triggered.
Usage | null
Token usage for the request.
string | null
The reason the model stopped generating. Common values: "stop", "length", "content_filter".
GatelitMeta
Gatelit-specific metadata.

Example


chatJson<T>(options)

Like chat(), but parses the response content as JSON and returns it as type T. Pair with outputSchema: { mode: "json_object" } or mode: "json_schema" to instruct the model to return valid JSON.
Accepts the same options as chat(). Throws a GatelitError with code "invalid_json" if the model returns content that cannot be parsed.

Example


stream(options)

Sends a chat request and returns an async generator that yields incremental StreamChunk values as the model produces output.
Accepts the same options as chat().

Returns: AsyncGenerator<StreamChunk>

string
required
Incremental text produced by this chunk. Empty string on the final (usage) chunk.
string | undefined
Resolved model identifier. May be undefined on early chunks.
string | null
Non-null on the final chunk. Common values: "stop", "length".
Usage | null
Token usage — only present on the final chunk.

Example


conversation(options)

Creates a Conversation tied to this client. The conversation accumulates the full message history and sends it with each request automatically.
string
required
Model in "provider/model-name" format.
string
Optional system prompt prepended to every request.
number
Maximum output tokens per request.
number
Sampling temperature applied to every request in the conversation.
See the Conversation reference for the full API.

Example


Error handling

All methods throw GatelitError when the gateway returns a non-2xx response.
string
Machine-readable error code returned by the gateway. For example: "unauthorized", "gateway_error", "provider_error", "invalid_json". See the Errors reference for the full list.
string
Human-readable error description.
number
HTTP status code from the gateway response.