# agent: Auto-select user model when there's no default \(\#54125\) · gitcafe/zed

[View on GitCafe](https://git.cafe/gitcafe/zed/commit/e92a40a9d897f0ecea7b0c51dae117a22580a333)

Repository: [gitcafe/zed](https://git.cafe/gitcafe/zed)

Visibility: public

Requested revision: e92a40a9d897f0ecea7b0c51dae117a22580a333

Requested commit: e92a40a9d897f0ecea7b0c51dae117a22580a333

Commit: e92a40a9d897f0ecea7b0c51dae117a22580a333

Tree: 03fa977181b38078e307f36e57ed4546c05b1efd

Author: Anthony Eid

Committer: GitHub

## Message

```
agent: Auto-select user model when there's no default (#54125)

Reimplements #36722 while fixing the race that required the revert in
#36932.

When no default model is configured, this picks an environment fallback
by authenticating all providers. It always prefers the Zed cloud
provider when it's authenticated, and waits for its models to load
before picking another provider as the fallback, so we don't flicker
from Zed models to Anthropic while sign-in is in flight.

The fallback is recomputed whenever provider state changes (via
`ProviderStateChanged`/`AddedProvider`/`RemovedProvider` events), so the
selection becomes correct as soon as cloud models arrive.

### What changed vs. the original PR

- `language_models::init` now owns `authenticate_all_providers`
(previously done in `LanguageModelPickerDelegate` and `agent`'s
`LanguageModels`).
- After all authentications settle, and on any subsequent provider state
change, `update_environment_fallback_model` recomputes the fallback.
- The fallback logic prefers Zed cloud: if the cloud provider is
authenticated, only use it (waiting for its models to load). Otherwise,
fall through to the first authenticated provider with a default or
recommended model.
- `LanguageModelRegistry::default_model()` falls back to
`environment_fallback_model` when no explicit default is set.
- Existing `Thread`s that are empty are updated to the new default when
`DefaultModelChanged` fires, so a blank thread started before sign-in
switches to Zed models once the user signs in.

Release Notes:

- agent: Automatically select a model when there's no selected model or configured default
```

## Parents

- [f9ca74e68d80052b26fa33636b2df3489d3b7841](https://git.cafe/gitcafe/zed/commit/f9ca74e68d80052b26fa33636b2df3489d3b7841?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zed/tree/e92a40a9d897f0ecea7b0c51dae117a22580a333?format=markdown)
