-
Notifications
You must be signed in to change notification settings - Fork 495
Feat/add litellm provider #653
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
RheagalFire
wants to merge
3
commits into
plastic-labs:main
Choose a base branch
from
RheagalFire:feat/add-litellm-provider
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,11 @@ | ||
| from .anthropic import AnthropicBackend | ||
| from .gemini import GeminiBackend | ||
| from .litellm import LiteLLMBackend | ||
| from .openai import OpenAIBackend | ||
|
|
||
| __all__ = [ | ||
| "AnthropicBackend", | ||
| "GeminiBackend", | ||
| "LiteLLMBackend", | ||
| "OpenAIBackend", | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,223 @@ | ||
| """LiteLLM provider backend. | ||
|
|
||
| Routes to 100+ LLM providers via a unified interface using provider-prefixed | ||
| model names (e.g. ``anthropic/claude-sonnet-4-6``, ``gemini/gemini-2.5-flash``). | ||
|
|
||
| Install: ``pip install litellm`` | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| import logging | ||
| from collections.abc import AsyncIterator | ||
| from typing import Any | ||
|
|
||
| from pydantic import BaseModel | ||
|
|
||
| from src.exceptions import ValidationException | ||
| from src.llm.backend import CompletionResult, StreamChunk, ToolCallResult | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class LiteLLMBackend: | ||
| """Provider backend wrapping litellm.acompletion.""" | ||
|
|
||
| def __init__(self, api_key: str | None = None, api_base: str | None = None) -> None: | ||
| self._api_key = api_key | ||
| self._api_base = api_base | ||
|
|
||
| def _base_kwargs(self) -> dict[str, Any]: | ||
| kwargs: dict[str, Any] = {"drop_params": True} | ||
| if self._api_key: | ||
| kwargs["api_key"] = self._api_key | ||
| if self._api_base: | ||
| kwargs["api_base"] = self._api_base | ||
| return kwargs | ||
|
|
||
| @staticmethod | ||
| def _import_litellm() -> Any: | ||
| try: | ||
| import litellm | ||
| except ModuleNotFoundError as exc: | ||
| raise ValidationException( | ||
| "LiteLLM transport requires optional dependency 'litellm'. " | ||
| "Install with: pip install honcho[litellm]" | ||
| ) from exc | ||
| return litellm | ||
|
|
||
| async def complete( | ||
| self, | ||
| *, | ||
| model: str, | ||
| messages: list[dict[str, Any]], | ||
| max_tokens: int, | ||
| temperature: float | None = None, | ||
| stop: list[str] | None = None, | ||
| tools: list[dict[str, Any]] | None = None, | ||
| tool_choice: str | dict[str, Any] | None = None, | ||
| response_format: type[BaseModel] | dict[str, Any] | None = None, | ||
| thinking_budget_tokens: int | None = None, | ||
| thinking_effort: str | None = None, | ||
| max_output_tokens: int | None = None, | ||
| extra_params: dict[str, Any] | None = None, | ||
| ) -> CompletionResult: | ||
| litellm = self._import_litellm() | ||
|
|
||
| params = self._build_params( | ||
| model=model, | ||
| messages=messages, | ||
| max_tokens=max_output_tokens or max_tokens, | ||
| temperature=temperature, | ||
| stop=stop, | ||
| tools=tools, | ||
| tool_choice=tool_choice, | ||
| response_format=response_format, | ||
| thinking_effort=thinking_effort, | ||
| extra_params=extra_params, | ||
| ) | ||
|
|
||
| response = await litellm.acompletion(**params) | ||
| return self._normalize_response(response) | ||
|
|
||
| async def stream( | ||
| self, | ||
| *, | ||
| model: str, | ||
| messages: list[dict[str, Any]], | ||
| max_tokens: int, | ||
| temperature: float | None = None, | ||
| stop: list[str] | None = None, | ||
| tools: list[dict[str, Any]] | None = None, | ||
| tool_choice: str | dict[str, Any] | None = None, | ||
| response_format: type[BaseModel] | dict[str, Any] | None = None, | ||
| thinking_budget_tokens: int | None = None, | ||
| thinking_effort: str | None = None, | ||
| max_output_tokens: int | None = None, | ||
| extra_params: dict[str, Any] | None = None, | ||
| ) -> AsyncIterator[StreamChunk]: | ||
| litellm = self._import_litellm() | ||
|
|
||
| params = self._build_params( | ||
| model=model, | ||
| messages=messages, | ||
| max_tokens=max_output_tokens or max_tokens, | ||
| temperature=temperature, | ||
| stop=stop, | ||
| tools=tools, | ||
| tool_choice=tool_choice, | ||
| response_format=response_format, | ||
| thinking_effort=thinking_effort, | ||
| extra_params=extra_params, | ||
| ) | ||
| params["stream"] = True | ||
|
|
||
| response_stream = await litellm.acompletion(**params) | ||
| finish_reason: str | None = None | ||
| async for chunk in response_stream: | ||
| if chunk.choices and chunk.choices[0].delta.content: | ||
| yield StreamChunk(content=chunk.choices[0].delta.content) | ||
| if chunk.choices and chunk.choices[0].finish_reason: | ||
| finish_reason = chunk.choices[0].finish_reason | ||
| usage = getattr(chunk, "usage", None) | ||
| if usage: | ||
| yield StreamChunk( | ||
| is_done=True, | ||
| finish_reason=finish_reason, | ||
| output_tokens=getattr(usage, "completion_tokens", None), | ||
| ) | ||
| return | ||
|
|
||
| if finish_reason: | ||
| yield StreamChunk(is_done=True, finish_reason=finish_reason) | ||
|
|
||
| def _build_params( | ||
| self, | ||
| *, | ||
| model: str, | ||
| messages: list[dict[str, Any]], | ||
| max_tokens: int, | ||
| temperature: float | None, | ||
| stop: list[str] | None, | ||
| tools: list[dict[str, Any]] | None, | ||
| tool_choice: str | dict[str, Any] | None, | ||
| response_format: type[BaseModel] | dict[str, Any] | None, | ||
| thinking_effort: str | None, | ||
| extra_params: dict[str, Any] | None, | ||
| ) -> dict[str, Any]: | ||
| params: dict[str, Any] = { | ||
| "model": model, | ||
| "messages": messages, | ||
| "max_tokens": max_tokens, | ||
| **self._base_kwargs(), | ||
| } | ||
| if temperature is not None: | ||
| params["temperature"] = temperature | ||
| if stop: | ||
| params["stop"] = stop | ||
| if tools: | ||
| params["tools"] = self._convert_tools(tools) | ||
| if tool_choice is not None: | ||
| params["tool_choice"] = tool_choice | ||
| if response_format is not None: | ||
| if isinstance(response_format, type) and issubclass( | ||
| response_format, BaseModel | ||
| ): | ||
| params["response_format"] = response_format | ||
| else: | ||
| params["response_format"] = response_format | ||
| if thinking_effort: | ||
| params["reasoning_effort"] = thinking_effort | ||
| if extra_params: | ||
| for key in ("top_p", "frequency_penalty", "presence_penalty", "seed"): | ||
| if key in extra_params: | ||
| params[key] = extra_params[key] | ||
| return params | ||
|
|
||
| @staticmethod | ||
| def _normalize_response(response: Any) -> CompletionResult: | ||
| usage = getattr(response, "usage", None) | ||
| message = response.choices[0].message | ||
| finish_reason = response.choices[0].finish_reason | ||
|
|
||
| tool_calls: list[ToolCallResult] = [] | ||
| for tc in getattr(message, "tool_calls", None) or []: | ||
| tool_input: dict[str, Any] = {} | ||
| if tc.function.arguments: | ||
| try: | ||
| tool_input = json.loads(tc.function.arguments) | ||
| except (json.JSONDecodeError, TypeError): | ||
| logger.warning( | ||
| "Malformed tool arguments for %s (id=%s)", | ||
| tc.function.name, | ||
| tc.id, | ||
| ) | ||
| tool_calls.append( | ||
| ToolCallResult(id=tc.id, name=tc.function.name, input=tool_input) | ||
| ) | ||
|
|
||
| return CompletionResult( | ||
| content=getattr(message, "content", "") or "", | ||
| input_tokens=getattr(usage, "prompt_tokens", 0) if usage else 0, | ||
| output_tokens=getattr(usage, "completion_tokens", 0) if usage else 0, | ||
| finish_reason=finish_reason or "stop", | ||
| tool_calls=tool_calls, | ||
| raw_response=response, | ||
| ) | ||
|
|
||
| @staticmethod | ||
| def _convert_tools(tools: list[dict[str, Any]]) -> list[dict[str, Any]]: | ||
| if not tools or tools[0].get("type") == "function": | ||
| return tools | ||
| return [ | ||
| { | ||
| "type": "function", | ||
| "function": { | ||
| "name": tool["name"], | ||
| "description": tool["description"], | ||
| "parameters": tool["input_schema"], | ||
| }, | ||
| } | ||
| for tool in tools | ||
| ] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🌐 Web query:
What is the exact parameter name and shape for setting an Anthropic thinking budget through litellm.acompletion in litellm 1.65?💡 Result:
In liteLLM 1.65, when you use litellm.completion(...)/litellm.acompletion(...) with Anthropic extended thinking, the exact parameter is named thinking and its shape is: thinking={"type": "enabled", "budget_tokens": } Example: response = litellm.acompletion( model="anthropic/claude-sonnet-4-6", messages=[{"role": "user", "content": "…"}], thinking={"type": "enabled", "budget_tokens": 1024},) liteLLM also documents an OpenAI-style alternative mapping: reasoning_effort is mapped to Anthropic output_config.effort (not budget_tokens) for Claude 4.6/4.7 models. If you specifically need a token budget for thinking, use the thinking parameter above, not reasoning_effort. [1][2][3]
Citations:
🏁 Script executed:
# First, let's examine the litellm.py file to understand its structure wc -l src/llm/backends/litellm.pyRepository: plastic-labs/honcho
Length of output: 95
🏁 Script executed:
Repository: plastic-labs/honcho
Length of output: 5776
🏁 Script executed:
Repository: plastic-labs/honcho
Length of output: 171
🏁 Script executed:
Repository: plastic-labs/honcho
Length of output: 3803
Forward
thinking_budget_tokensparameter or document that it's ignored.complete()andstream()acceptthinking_budget_tokensbut never forward it to_build_params()or use it. LiteLLM requiresthinking={"type": "enabled", "budget_tokens": ...}for Anthropic extended thinking with token budgets. Without plumbing this parameter, callers will silently get incorrect behavior. Either construct and pass thethinkingparameter based onthinking_budget_tokens, or explicitly log/document that this backend ignores it.Also applies to:
stream()(lines 95–113) and_build_params()(lines 135–176)🤖 Prompt for AI Agents