-
Notifications
You must be signed in to change notification settings - Fork 64
fix: enable LiteLLM virtual key cost tracking for default agent #604
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -155,3 +155,22 @@ def set_current_virtual_key(key: str | None) -> None: | |
| def get_current_virtual_key() -> str | None: | ||
| """Return the virtual key for the current worker thread, or None.""" | ||
| return getattr(_thread_local, "virtual_key", None) | ||
|
|
||
|
|
||
| def apply_virtual_key(llm): # type: ignore[no-untyped-def] | ||
| """Return an LLM config copy with the per-instance virtual key as api_key. | ||
|
|
||
| If no virtual key is active for this thread, returns the original config | ||
| unchanged. This is thread-safe: ``model_copy`` creates a new instance | ||
| and ``get_current_virtual_key`` reads from ``threading.local``. | ||
|
|
||
| Use this when creating a default (non-ACP) ``Agent`` so that all LLM | ||
| calls go through the proxy with the per-instance virtual key, enabling | ||
| accurate per-instance cost tracking. | ||
| """ | ||
| virtual_key = get_current_virtual_key() | ||
| if virtual_key is None: | ||
| return llm | ||
| from pydantic import SecretStr | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Suggestion: Move import to top of file. Per project guidelines: "Place all imports at the top of the file unless... circular imports, conditional imports, or imports that need to be delayed for specific reasons." No circular import risk here since from pydantic import SecretStr |
||
|
|
||
| return llm.model_copy(update={"api_key": SecretStr(virtual_key)}) | ||
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.
🟠 Important: Missing type hints violates project standards.
Per
AGENTS.md: "Avoid# type: ignoreunless absolutely necessary". The LLM type is available from the SDK:You'll need to add
from openhands.sdk.llm import LLMat the top of the file.