Skip to main content

View on GitHub

Built in Rust and integrated with Python, Node.js, and native Rust, the Performance Client handles concurrent POST requests efficiently. It releases the Python GIL while executing requests, enabling simultaneous sync and async usage. Benchmarks show the Performance Client reaches 1200+ requests per second per client. Use it with Baseten deployments or third-party providers like OpenAI. benchmarks

Install the client

Install the Performance Client:
Terminal

Get started

To initialize the Performance Client in Python, import the class and provide your base URL and API key:
quickstart.py
The client also works with third-party providers like OpenAI by replacing the base_url.

Advanced setup

Configure HTTP version selection and connection pooling for optimal performance.
To configure HTTP version and connection pooling in Python, use the http_version parameter and HttpClientWrapper:
advanced_setup.py

Core features

Embeddings

The client provides efficient embedding requests with configurable batching, concurrency, and latency optimizations. Compatible with BEI.
To generate embeddings with Python, configure a RequestProcessingPreference and call client.embed():
embed.py
For async usage, call await client.async_embed(input=texts, model="my_model", preference=preference).

Generic batch POST

Send HTTP requests to any URL with any JSON payload. Compatible with Engine-Builder-LLM and other models. Set stream=False for SSE endpoints.
To send batch POST requests with Python, define your payloads and call client.batch_post():
batch_post.py
Supported methods: GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS.For async usage, call await client.async_batch_post(url_path, payloads, preference, method).

Reranking

Rerank documents by relevance to a query. Compatible with BEI, BEI-Bert, and text-embeddings-inference reranking endpoints.
To rerank documents with Python, provide a query and list of documents to client.rerank():
rerank.py
For async usage, call await client.async_rerank(query, texts, model, return_text, preference).

Classification

Classify text inputs into categories. Compatible with BEI and text-embeddings-inference classification endpoints.
To classify text with Python, provide a list of inputs to client.classify():
classify.py
For async usage, call await client.async_classify(inputs, model, preference).

Advanced features

Configure RequestProcessingPreference

The RequestProcessingPreference class provides unified configuration for all request processing parameters.
To configure request processing in Python, create a RequestProcessingPreference instance:
preference.py

Parameter reference

Hedge delay sends duplicate requests after a specified delay to reduce p99 latency. After the delay, the request is cloned and raced against the original. The 429 and 5xx errors are always retried automatically.

Retry configuration

HTTP status-code retries are controlled by max_retries (maxRetries in JavaScript), which is separate from retry_budget_pct. By default, 408, 409, 429, and 500 through 599 are retried. Set max_retries to 0 to disable these retries. Use non_retryable_status_codes (nonRetryableStatusCodes in JavaScript) to opt specific status codes out of the default policy. For example, pass {529} in Python or [529] in JavaScript to stop retrying 529 responses. Backoff starts at initial_backoff_ms (initialBackoffMs in JavaScript), multiplies by 4 after each retry, caps at 45000 milliseconds, and adds up to 99 milliseconds of jitter.

Automatic timeout headers

The Performance Client sends timeout headers with every request so the server can cancel work that exceeds the client’s timeout and return an error before the client gives up. Two headers are derived from the timeout_s setting in RequestProcessingPreference:
  • Request-Timeout-Ms: relative timeout in milliseconds, rounded up.
  • Request-Deadline-Ms: absolute deadline as a Unix timestamp in milliseconds.
For example, with timeout_s=30.5, the client sends:

Select HTTP version

HTTP/1.1 is recommended for high concurrency workloads.
To select the HTTP version in Python, use the http_version parameter:
http_version.py

Share connection pools

Share connection pools across multiple client instances to reduce overhead when connecting to multiple endpoints.
To share a connection pool in Python, create an HttpClientWrapper and pass it to each client:
shared_pool.py

Cancel operations

Cancel long-running operations using CancellationToken. The token provides immediate cancellation, resource cleanup, Ctrl+C support, token sharing across operations, and status checking with is_cancelled().
To cancel operations in Python, create a CancellationToken and pass it to your preference:
cancel.py

Handle errors

The client raises standard exceptions for error conditions:
  • HTTPError: Authentication failures (403), server errors (5xx), endpoint not found (404).
  • Timeout: Request or total operation timeout based on timeout_s or total_timeout_s.
  • ValueError: Invalid input parameters (empty input list, invalid batch size, inconsistent embedding dimensions).
To handle errors in Python, catch the appropriate exception types:
handle_errors.py

Configure the client

Environment variables

  • BASETEN_API_KEY: Your Baseten API key. Also checks OPENAI_API_KEY as fallback.
  • PERFORMANCE_CLIENT_LOG_LEVEL: Logging level. Overrides RUST_LOG. Valid values: trace, debug, info, warn, error. Default: warn.
  • PERFORMANCE_CLIENT_REQUEST_ID_PREFIX: Custom prefix for request IDs. Default: perfclient.

Configure logging

To set the logging level, use the PERFORMANCE_CLIENT_LOG_LEVEL environment variable:
Terminal
The PERFORMANCE_CLIENT_LOG_LEVEL variable takes precedence over RUST_LOG.

Use with Rust

The Performance Client is also available as a native Rust library. To use the Performance Client in Rust, add the dependencies and create a PerformanceClientCore instance:
main.rs
Add these dependencies to your Cargo.toml:
Cargo.toml