Skip to main content
praisonai-ts picks up a changed OPENAI_API_KEY or OPENAI_BASE_URL on the next call — no restart, no code change. The env-only OpenAI client caches on client identity (apiKey + baseURL). Rotate either and the next call rebuilds automatically — the exact flow a settings screen needs when a user pastes a new key.

Quick Start

1

Rotate the key in your settings screen

Set the env var; the next call uses the new key.
2

Point at a different gateway

Set OPENAI_BASE_URL; the next call rebuilds against the new endpoint.
3

Force a rebuild explicitly

Call resetOpenAIClient() to rebuild on the next call — useful in tests and after a bulk settings change.

How It Works

The env-only client keys its cache on apiKey + baseURL. A settings-screen edit to either env var changes that identity, so the next getOpenAIClient() rebuilds.
OPENAI_BASE_URL is honoured in the env-only path. Exporting it works for the module-level convenience functions and any Agent built from env only — the same way an explicit baseURL on the config already did.

When Does This Apply?

Rotation applies only to the shared env-only client. Per-agent credentials never touch that cache. An Agent built with an explicit apiKey or baseURL gets its own dedicated client, so a later env-var change does not affect it. Rotate by constructing a new Agent with the new value.

Concurrency

A resetOpenAIClient() or credential change that races an in-flight request is safe. Each caller keeps the client it built for its own identity — an overlapping call that swaps the key never hands back another call’s client, and a reset mid-flight never yields null.

Security

The identity that keys the cache contains the secret key. praisonai-ts never logs it — do not add logging that prints it either.

API Reference

Two exports back the rotation contract. The recommended user surface stays new Agent(...) and agent.chat(...).
getOpenAIClient(): Promise<OpenAI> is exported so tests and advanced integrations can reach the shared env-only client. It throws OPENAI_API_KEY not found in environment variables when no key is set. Prefer new Agent(...) for application code.

Common Patterns

Simple rotation — no code change from the SDK caller
Change the gateway at runtime
Explicit rebuild — tests and bulk settings updates

Best Practices

Pass apiKey / baseURL on each Agent to isolate tenants. Each agent gets its own client, so one tenant’s rotation never affects another.
The next call rebuilds when the key or base URL changes. There is no need to call resetOpenAIClient() on every key update.
The cache identity embeds the secret. Keep it out of logs and error messages.
After changing several settings at once in a UI, a single resetOpenAIClient() is enough — the next call rebuilds once.

Agent

The core Agent class and credential options

Import Safety (JS)

Lazy credential reads and runtime safety

Browser & Webview Runtimes

Run agents in browsers, Tauri, Electron, and React Native