await. The package includes TypeScript declarations.
See the Performance Client overview for shared batching, retry, and connection settings.
Installation
First request
Set your API key, an embeddings deployment URL, and themodel value expected by that deployment:
embeddings.mjs. The .mjs extension enables ES modules and top-level await.
embeddings.mjs
node embeddings.mjs. A successful response prints 2 embeddings, followed by token usage and elapsed time. The operation examples below use the same imports and a client configured for the corresponding deployment.
PerformanceClient
Base URL for requests. The client appends the request path. For an embeddings deployment, use the URL ending in
/sync, without /v1/embeddings.Inference API key. When omitted, the client checks
BASETEN_API_KEY, then OPENAI_API_KEY. Construction fails if no key is available.Shared HTTP connection pool. When supplied, its HTTP version and proxy configuration take precedence.
Set of endpoints the client can send requests to. The package doesn’t export
Endpoint or EndpointPool, so you can’t create a pool through the package import.getClientWrapper
client.getClientWrapper() returns the HttpClientWrapper used by the client. Pass it to another client to share connections.
Inference methods
Optional arguments are positional. Passundefined or null to skip an argument. There are no separate synchronous or asyncEmbed methods.
embed
Generates embeddings for the input texts.
Use
"float". Base64 embeddings become empty arrays in the returned response. If omitted, the server selects the format.Batching, concurrency, timeout, and retry settings for this call. See Request preferences.
rerank
Scores texts against a query.
Batching, concurrency, timeout, and retry settings for this call. See Request preferences.
model, rawScores, returnText, truncate, and truncationDirection.
Returns a reranking response.
Configure client for a reranking deployment, then score the texts:
classify
Assigns labels and scores to the input texts.
Nonempty list of strings. The client sends each string as a one-element list in the request’s
inputs field.Batching, concurrency, timeout, and retry settings for this call. See Request preferences.
client for a classification deployment, then classify the texts:
batchSystemOne
Sends one System One decision request per input object. Configure client with a base URL that serves /v1/systemone, without that path appended.
For supported models and complete request examples, see Decision models.
Nonempty array of System One request bodies. Each object contains
state and questions. Include model when the endpoint requires a model slug. Additional fields pass through to the server.Concurrency, timeout, retry, and header settings. Each object is a separate POST request;
batchSize doesn’t combine them.data array contains the server responses in input order, with timing and response headers on the enclosing object. An empty array rejects the promise.
batchPost
Sends one HTTP request per payload and returns responses in input order.
Nonempty array of JSON-compatible values.
batchSize and maxCharsPerRequest don’t combine them into a single request.Concurrency, timeout, retry, and header settings. Set custom headers with
extraHeaders; batchPost has no separate headers argument. See Request preferences.HTTP method. Also accepts
"GET", "PUT", "PATCH", "DELETE", "HEAD", and "OPTIONS". Use uppercase names. The client sends request bodies for POST, PUT, and PATCH. For DELETE, HEAD, and OPTIONS, the response data contains empty objects rather than decoded response bodies.client for a deployment that serves /v1/completions, then send non-streaming completion requests:
Request preferences
Pass aRequestProcessingPreference to an inference method to set batching, concurrency, timeouts, and retries for that call. Its constructor accepts positional arguments in this order:
undefined to retain an argument’s default when setting a later argument.
Maximum concurrent requests. Must be 1 to 1,024, or 1 to 512 when
batchSize is below 16.Maximum inputs per embedding, reranking, or classification request. Accepts 1 to 1,024.
Character threshold for splitting text batches. Accepts 50 to 1,048,576. The client sends any text that exceeds the threshold intact in its own batch.
Delay in seconds before sending a duplicate request. Defaults to
null, which disables hedging. When set, must be at least 0.045 seconds and less than timeoutS - 0.045.Timeout for the entire operation. Defaults to
null. Must be at least timeoutS when supplied.Fraction used to calculate the operation’s hedge budget. Defaults to
0.10, or 10%. Accepts 0 to 3.Fraction used to calculate the operation’s budget for timeout and network retries. Defaults to
0.05, or 5%. HTTP-status retries don’t consume this budget. Accepts 0 to 3.Cancellation state. Unset when omitted. Doesn’t prevent or interrupt inference requests. See
CancellationToken.Accepts and stores a key but doesn’t change request authentication. Defaults to
null. Set apiKey on PerformanceClient instead.cancelToken, which has no public getter. Only traceContext has a setter. To change other preferences, construct a new instance.
The client validates request preferences when you call an operation.
Response objects
Responses are plain JavaScript objects with snake_case property names, such astotal_time. The TypeScript return type is Promise<any>; the package doesn’t export response classes or a numpy() helper.
All responses include these fields:
Embedding response
Reranking response
Classification response
Batch response
Decoded response payloads in input order. See
batchPost for HTTP methods that return empty objects.Helper types
The package entry point exportsPerformanceClient, RequestProcessingPreference, HttpClientWrapper, and CancellationToken. Import these classes from @basetenlabs/performance-client. CommonJS applications can also use require("@basetenlabs/performance-client").
HttpClientWrapper
new HttpClientWrapper(httpVersion?, proxy?) creates a reusable HTTP connection pool. Pass the wrapper to the PerformanceClient constructor.
Set BASETEN_SECOND_BASE_URL to another deployment URL, then share the wrapper between clients:
CancellationToken
new CancellationToken() creates a token. Pass it as the twelfth RequestProcessingPreference argument. token.cancel() sets its cancelled state. A cancelled token can’t be reset.
The Node.js inference methods don’t check or poll the token. Calling cancel() doesn’t prevent requests from starting or interrupt requests in flight. Use timeoutS and totalTimeoutS to bound request duration.
TraceContext
TraceContext is a TypeScript interface, not a runtime constructor:
preference.traceContext. Don’t also set traceparent or tracestate in extraHeaders; the client rejects that combination.
To export client spans to an OpenTelemetry collector, configure client tracing.
Endpoint
Endpoint configures a base URL and its health checks. It appears only in the TypeScript declarations, so you can’t construct it through the package import:
Ignore a timeout from the deployment check when deciding whether the endpoint is healthy.
Ignore a timeout from the deep health check when deciding whether the endpoint is healthy.
EndpointPool
The TypeScript declaration new EndpointPool(endpoints, endpointWeights?) groups endpoints for a client.
Endpoint weights. Defaults to equal values. Custom weights must match the endpoint count, be finite and nonnegative, and include at least one positive value.
Endpoint, EndpointPool appears only in the type declarations. You can’t construct it through the package import or pass a new pool to PerformanceClient.
Errors
Inference methods reject their promises when a request or validation fails.InvalidArg for invalid parameters. HTTP, network, connection, serialization, timeout, and cancellation failures have the code GenericFailure.