# apply_stored_credentials

> **Function** in `deepagents_code`

📖 [View in docs](https://reference.langchain.com/python/deepagents-code/model_config/apply_stored_credentials)

Export this provider's stored key *and endpoint* into `os.environ`.

LangChain's chat-model factories read credentials from process env vars,
so a stored key only takes effect once it's copied onto the env var name
registered for that provider. This is a no-op when the provider has no
env-var mapping (custom auth) or no stored credential.

The key env var is overwritten whether or not it was already set, matching
the precedence rule documented on `resolve_provider_credential`: a
credential the user typed in `/auth` is the most recent deliberate
action and should take effect.

Because a key and its endpoint are a coherent pair (a gateway key only
works against the gateway URL; a provider-native key only against the
provider's own endpoint), the base URL is applied atomically with the key:

- A stored `base_url` is written to the provider's canonical base-URL env
    var, and every *other* base-URL name the SDK reads is cleared so an
    inherited gateway URL can't leak through an alternate variable.
- No stored `base_url` (the user left the field blank) clears *all* of the
    provider's base-URL env vars, so the SDK falls back to the provider
    default rather than an inherited gateway URL. This is what prevents a
    personal key from being shipped to the gateway.

Only the unprefixed canonical names are written, so an explicit
`DEEPAGENTS_CODE_{VAR}` override still wins via `resolve_env_var`.

## Signature

```python
apply_stored_credentials(
    provider: str,
) -> bool
```

## Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `provider` | `str` | Yes | Provider name. |

## Returns

`bool`

`True` if a stored key was applied, `False` otherwise.

---

[View source on GitHub](https://github.com/langchain-ai/deepagents/blob/0412009c5441bef8d75a427e1da8909e33ab5b56/libs/code/deepagents_code/model_config.py#L1999)