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 onapiKey + 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. AnAgent 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
AresetOpenAIClient() 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 staysnew Agent(...) and agent.chat(...).
Advanced: getOpenAIClient()
Advanced: getOpenAIClient()
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 callerBest Practices
Prefer per-agent credentials in multi-tenant apps
Prefer per-agent credentials in multi-tenant apps
Pass
apiKey / baseURL on each Agent to isolate tenants. Each agent gets its own client, so one tenant’s rotation never affects another.Trust the automatic rebuild in single-tenant apps
Trust the automatic rebuild in single-tenant apps
The next call rebuilds when the key or base URL changes. There is no need to call
resetOpenAIClient() on every key update.Never log the identity string or API key
Never log the identity string or API key
The cache identity embeds the secret. Keep it out of logs and error messages.
One reset after a bulk settings change
One reset after a bulk settings change
After changing several settings at once in a UI, a single
resetOpenAIClient() is enough — the next call rebuilds once.Related
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

