opencode icon indicating copy to clipboard operation
opencode copied to clipboard

feat(opencode): add LiteLLM provider with auto model discovery

Open balcsida opened this issue 9 months ago • 17 comments

Issue for this PR

Closes #13891

Type of change

  • [ ] Bug fix
  • [x] New feature
  • [ ] Refactor / code improvement
  • [ ] Documentation

What does this PR do?

Adds a native litellm provider that auto-discovers models from a LiteLLM proxy at startup. Previously users had to manually define every model in opencode.json — now setting LITELLM_API_KEY and LITELLM_HOST is enough.

Discovery: Fetches /model/info for rich metadata (pricing, limits, capabilities). Falls back to the standard /models endpoint for older LiteLLM versions or non-LiteLLM OpenAI-compatible proxies.

What gets mapped from /model/info:

  • Per-token pricing → per-million-token (including over-200k tier)
  • Context/output limits
  • Capabilities: reasoning, vision, PDF, audio, video, tool calling
  • Temperature support via supported_openai_params
  • Interleaved thinking inference for Claude/Anthropic models

Reasoning transforms: Claude models behind LiteLLM get thinking budget variants, other models get reasoningEffort. This prevents false-positive reasoning param injection for aliased models (e.g. o3-custom → Mistral).

Files changed:

  • litellm.ts (new): Discovery module — fetches, parses, maps model metadata
  • provider.ts: Seeds litellm provider from env vars; custom loader calls discovery and injects models (user config takes precedence)
  • transform.ts: LiteLLM-specific reasoning variant logic
  • llm.ts: Adds explicit providerID === "litellm" to proxy detection
Env var Description Default
LITELLM_API_KEY API key for the proxy —
LITELLM_HOST Base URL of the proxy http://localhost:4000
LITELLM_BASE_URL Alias for LITELLM_HOST —
LITELLM_CUSTOM_HEADERS JSON string of extra headers {}
LITELLM_TIMEOUT Discovery timeout in ms 5000

Builds on ideas from #13896 (fallback endpoint, over-200k pricing, temperature detection) and #14277 (clean fallback chain). Supersedes my earlier #14202 which was auto-closed for not following the PR template.

How did you verify your code works?

  1. Set LITELLM_API_KEY + LITELLM_HOST pointing to a running LiteLLM proxy
  2. Run opencode — LiteLLM models appear in model picker
  3. Select a model and send a message — streaming works
  4. Test aliased reasoning model (e.g. o3-custom → Mistral) — reasoning params NOT injected
  5. Test actual Claude reasoning model — thinking variants work
  6. Test without proxy running — graceful fallback, provider hidden
  7. Test with older proxy (no /model/info) — falls back to /models with sensible defaults

Screenshots / recordings

N/A — no UI changes

Checklist

  • [x] I have tested my changes locally
  • [x] I have not included unrelated changes in this PR

balcsida avatar Feb 20 '26 16:02 balcsida

The following comment was made by an LLM, it may be inaccurate:

Based on my search, I found two related PRs that are NOT duplicates but are closely related:

  1. PR #14277: feat(opencode): add dynamic model fetching for OpenAI-compatible provider

    • Related because the current PR builds on ideas from this PR (clean fallback chain mentioned in description)
  2. PR #13896: feat(opencode): add auto loading models for litellm providers

    • Related because the current PR explicitly builds on ideas from this PR (fallback endpoint, over-200k pricing, temperature detection mentioned in description)

These PRs appear to be related work in the same area but are separate features. PR #14468 (the current PR) is a distinct, more comprehensive implementation of the LiteLLM provider with auto-discovery, and it references these earlier related PRs rather than duplicating them.

No duplicate PRs found.

github-actions[bot] avatar Feb 20 '26 16:02 github-actions[bot]

lgtm, but test fails. check e2e. this test fails: e2e/projects/projects-switch.spec.ts:49:1 › switching back to a project opens the latest workspace session

Did you try testing locally? bun test:e2e:local

alexyaroshuk avatar Feb 22 '26 00:02 alexyaroshuk

@alexyaroshuk, yes, sorry, fixed all the tests

balcsida avatar Feb 22 '26 21:02 balcsida

👍

kkugot avatar Feb 26 '26 09:02 kkugot

@alexyaroshuk, may I have your blessing on this? 🥺

balcsida avatar Feb 26 '26 21:02 balcsida

@adamdotdevin can this one get a look?

alexyaroshuk avatar Feb 26 '26 23:02 alexyaroshuk

:shipit: maybe?

kkugot avatar Mar 04 '26 09:03 kkugot

@alexyaroshuk @adamdotdevin any luck to get it merged soon ?

ELBouslama avatar Mar 06 '26 12:03 ELBouslama

@balcsida please rebase, there are some conflicts now. @alexyaroshuk @adamdotdevin could you review please? This will make so many opencode users in enterprise world very happy. Thanks!

kkugot avatar Mar 13 '26 10:03 kkugot

Hey @kkugot

Thanks for the ping, I rebased the branch and fixed the conflicts, but dev currently fails, so tests for this PR will fail as well.

While I understand how this feature is a very nice to have in an enterprise environment (it is exactly the same reason why I sent this PR - we would like to use this as well), please kindly don't spam alexyaroshuk and adamdotdevin - I would rather not like put additional pressure on them.

Kind regards, @balcsida

balcsida avatar Mar 13 '26 11:03 balcsida

I would wait until this is cleared up:

https://github.com/BerriAI/litellm/issues/24512

janus-reith avatar Mar 24 '26 17:03 janus-reith

litellm is compromised https://github.com/BerriAI/litellm/issues/24512

zacharymichaelmoore avatar Mar 25 '26 08:03 zacharymichaelmoore

Any eta before this is merged?

jamie-dit avatar Apr 04 '26 00:04 jamie-dit

I don’t mean to add any pressure, but I’d really appreciate an estimated timeline for this.

ELBouslama avatar Apr 07 '26 12:04 ELBouslama

Any ETA for this? This would be really useful

jamie-dit avatar Apr 25 '26 01:04 jamie-dit

I packaged up opencode with this PR merged on my fork until this gets merged: https://github.com/balcsida/opencode/releases

Also made available on my Homebrew tap for your convenience:

brew tap balcsida/tap
brew install balcsida/tap/opencode-fork

Please keep in mind that these are no way official releases and while I'm doing my best effort to keep it up to date, kindly expect some lag behind the official releases.

balcsida avatar Apr 29 '26 13:04 balcsida

what do we need to do, to merge this in?

krrish-berri-2 avatar May 07 '26 16:05 krrish-berri-2

Guys, what's going on here? It's been five months, and it feels like this isn't being prioritized. Could one of the maintainers who really cares about the the open-source community please take a look and review this? At this point, the long delay is starting to feel intentional.

ELBouslama avatar Jul 06 '26 10:07 ELBouslama

I'm tired

balcsida avatar Jul 16 '26 18:07 balcsida

Sorry @balcsida

If it makes you feel better, I've been tracking your PR since the start. Real shame OpenCode ignored this PR.

jamie-dit avatar Jul 17 '26 07:07 jamie-dit