client_cache
langroid/language_models/client_cache.py
Client caching/singleton pattern for LLM clients to prevent connection pool exhaustion.
wrap_api_key_provider_async(provider)
¶
Wrap a sync API-key provider for use with AsyncOpenAI.
AsyncOpenAI awaits its api_key callable, so a plain sync
provider must be wrapped in an async function. The sync provider is run
in a worker thread, so a blocking token refresh cannot stall the event
loop. Providers must therefore be thread-safe -- the sync client may
call them from arbitrary threads anyway.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
provider
|
Callable[[], str]
|
Callable returning a (possibly short-lived) API key. |
required |
Returns:
| Type | Description |
|---|---|
Callable[[], Awaitable[str]]
|
Async callable returning the same key. |
Source code in langroid/language_models/client_cache.py
get_openai_client(api_key, base_url=None, organization=None, timeout=120.0, default_headers=None, http_client=None, http_client_config=None)
¶
Get or create a singleton OpenAI client with the given configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
api_key
|
Union[str, Callable[[], str]]
|
OpenAI API key, or a callable returning a fresh key (for short-lived rotating credentials); a callable is resolved per-request by the OpenAI client and is excluded from the cache key (the cache is keyed on the provider's identity) |
required |
base_url
|
Optional[str]
|
Optional base URL for API |
None
|
organization
|
Optional[str]
|
Optional organization ID |
None
|
timeout
|
Union[float, Timeout]
|
Request timeout |
120.0
|
default_headers
|
Optional[Dict[str, str]]
|
Optional default headers |
None
|
http_client
|
Optional[Any]
|
Optional httpx.Client instance |
None
|
http_client_config
|
Optional[Dict[str, Any]]
|
Optional config dict for creating httpx.Client |
None
|
Returns:
| Type | Description |
|---|---|
OpenAI
|
OpenAI client instance |
Source code in langroid/language_models/client_cache.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | |
get_async_openai_client(api_key, base_url=None, organization=None, timeout=120.0, default_headers=None, http_client=None, http_client_config=None)
¶
Get or create a singleton AsyncOpenAI client with the given configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
api_key
|
Union[str, Callable[[], str]]
|
OpenAI API key, or a callable returning a fresh key (for short-lived rotating credentials); a callable is resolved per-request by the OpenAI client and is excluded from the cache key (the cache is keyed on the provider's identity) |
required |
base_url
|
Optional[str]
|
Optional base URL for API |
None
|
organization
|
Optional[str]
|
Optional organization ID |
None
|
timeout
|
Union[float, Timeout]
|
Request timeout |
120.0
|
default_headers
|
Optional[Dict[str, str]]
|
Optional default headers |
None
|
http_client
|
Optional[Any]
|
Optional httpx.AsyncClient instance |
None
|
http_client_config
|
Optional[Dict[str, Any]]
|
Optional config dict for creating httpx.AsyncClient |
None
|
Returns:
| Type | Description |
|---|---|
AsyncOpenAI
|
AsyncOpenAI client instance |
Source code in langroid/language_models/client_cache.py
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | |
get_groq_client(api_key)
¶
Get or create a singleton Groq client with the given configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
api_key
|
str
|
Groq API key |
required |
Returns:
| Type | Description |
|---|---|
Groq
|
Groq client instance |
Source code in langroid/language_models/client_cache.py
get_async_groq_client(api_key)
¶
Get or create a singleton AsyncGroq client with the given configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
api_key
|
str
|
Groq API key |
required |
Returns:
| Type | Description |
|---|---|
AsyncGroq
|
AsyncGroq client instance |
Source code in langroid/language_models/client_cache.py
get_cerebras_client(api_key)
¶
Get or create a singleton Cerebras client with the given configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
api_key
|
str
|
Cerebras API key |
required |
Returns:
| Type | Description |
|---|---|
Cerebras
|
Cerebras client instance |
Source code in langroid/language_models/client_cache.py
get_async_cerebras_client(api_key)
¶
Get or create a singleton AsyncCerebras client with the given configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
api_key
|
str
|
Cerebras API key |
required |
Returns:
| Type | Description |
|---|---|
AsyncCerebras
|
AsyncCerebras client instance |
Source code in langroid/language_models/client_cache.py
prune_cache(max_age_seconds)
¶
Remove cache entries whose last-used time exceeds max_age_seconds.
Evicted clients are not closed here because they may still be serving
in-flight requests. Cleanup is handled by the atexit handler and the
garbage collector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_age_seconds
|
float
|
Maximum age (in seconds) for cache entries to keep. Entries older than this value are removed. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of cache entries removed. |