litellm/tests/unit/proxy/lens/test_inference.py
ishaan-berri dd31692282
feat(lens): always-on investigations with findings and investigations tables (#44418)
* feat(lens): record run steps, trigger and exact run windows on investigations

* feat(lens): scan only traces since the last run and keep a capped step log

* feat(lens): log each analysis model call with its model, tokens and cost

* feat(lens): accept agent and time window on run now and add turn-all-on

* test(lens): cover new-traces-only windows, manual runs and the step cap

* test(lens): cover run now overrides and turning paused investigations on

* chore(ui): regenerate api types for lens run steps and run now options

* feat(lens): group open findings into one row per problem with agent filters

* test(lens): cover the findings table grouping, filters and schedule labels

* feat(lens): add a findings table across all investigations

* feat(lens): show a live step feed with the model behind each call

* feat(lens): offer turning all paused investigations on

* feat(lens): let run now pick an agent and time window

* test(lens): cover run now request building

* feat(lens): show the step feed and run now dialog on an investigation

* feat(lens): open run now choices instead of running immediately

* feat(lens): open findings first and peek a finding without leaving the table

* feat(lens): fold investigation actions into the findings toolbar

* feat(lens): show each investigation's schedule and open findings

* feat(lens): name the agent on a finding

* feat(lens): keep new investigations watching every 15 minutes by default

* feat(lens): show the watch schedule outside advanced options

* feat(lens): send run now options and turn-all-on from the dashboard

* test(lens): give demo runs steps and a trigger

* feat(lens): let the findings table fill the screen

* test(lens): add steps and trigger to progress fixtures

* test(lens): add steps and trigger to status fixtures

* test(lens): cover the default watch schedule in setup

* test(lens): cover run now choices from an investigation

* test(lens): open saved investigations from the manage view

* feat(lens): use one tab bar for traces, findings and investigations

* feat(lens): place page actions on the lens tab row

* feat(lens): drop the nested tabs and edit investigations in place

* feat(lens): show investigations as a table with run now and edit

* feat(lens): name each findings row for screen readers

* test(lens): open saved investigation links on findings

* test(lens): reach findings and investigations from the top tabs

* fix(lens): mark run now jobs manual and keep them from moving the scheduled scan

* fix(lens): keep run now since-last-run windows even with an agent override

* test(lens): cover that manual runs never skip scheduled traces

* test(lens): cover run now windows with agent and lookback overrides

* fix(lens): group findings without Map.groupBy and expose sampled runs

* fix(lens): open older findings and review every merged copy from one row

* fix(lens): hide edit and run now from read-only viewers

* chore(lens): drop restating comments from the findings table

* chore(lens): drop restating comments from the step feed

* chore(lens): drop restating comments from the paused banner

* chore(lens): drop restating comments from header actions

* chore(lens): drop restating comments from run now

* test(lens): cover merged findings and read-only investigation rows

* fix(lens): record a model step even when the response has no usage

* test(lens): cover model steps with and without reported usage

* fix(lens): keep a merged finding open when one of its updates fails

* refactor(lens): accept update results from the investigations view

* refactor(lens): accept update results in investigation actions

* test(lens): cover retrying a merged finding after a failed update
2026-10-03 20:27:02 +00:00

137 lines
5.9 KiB
Python

from typing import Final
import pytest
from fastapi import HTTPException
import litellm
from litellm.proxy.lens.inference import Deployment, DeploymentParams, completion_charge, model_step, quote
from litellm.proxy.lens.models import ModelRequest
from litellm.types.utils import ModelResponse
def test_missing_optional_price_tiers_use_base_rates(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(litellm, "model_cost", {**litellm.model_cost})
litellm.register_model(
model_cost={
"openai/lens-base-rate-test": {
"litellm_provider": "openai",
"mode": "chat",
"max_output_tokens": 16384,
"input_cost_per_token": 0.001,
"output_cost_per_token": 0.002,
"input_cost_per_token_above_200k_tokens": None,
"output_cost_per_token_above_200k_tokens": None,
"input_cost_per_token_above_128k_tokens": None,
"output_cost_per_token_above_128k_tokens": None,
}
}
)
deployment: Final = Deployment(litellm_params=DeploymentParams(model="openai/lens-base-rate-test"))
explicit: Final = Deployment(
litellm_params=DeploymentParams(
model="openai/lens-base-rate-test",
input_cost_per_token=0.001,
output_cost_per_token=0.002,
max_tokens=16384,
)
)
assert quote((deployment,), "Answer the question") == quote((explicit,), "Answer the question")
def test_unpriced_model_requires_explicit_rates() -> None:
deployment: Final = Deployment(litellm_params=DeploymentParams(model="openai/lens-unpriced-test"))
with pytest.raises(HTTPException) as error:
quote((deployment,), "Answer the question")
assert error.value.status_code == 400
assert "input_cost_per_token" in error.value.detail
assert "output_cost_per_token" in error.value.detail
def test_custom_priced_model_charges_reported_tokens() -> None:
deployment: Final = Deployment(
litellm_params=DeploymentParams(
model="openai/lens-test", input_cost_per_token=0.001, output_cost_per_token=0.002, max_tokens=16384
)
)
response: Final = ModelResponse(
model="lens-test", usage={"prompt_tokens": 20, "completion_tokens": 10, "total_tokens": 30}
)
assert completion_charge((deployment,), response, 10) == pytest.approx(0.04)
assert quote((deployment,), "hello") > 0.04
@pytest.mark.parametrize("capacity", (8192, 65536, 128000))
def test_output_allowance_and_budget_follow_the_models_capacity(capacity: int, monkeypatch: pytest.MonkeyPatch) -> None:
from litellm.proxy.lens.inference import output_tokens
monkeypatch.setattr(litellm, "model_cost", {**litellm.model_cost})
litellm.register_model(
model_cost={
"openai/lens-capacity-test": {
"litellm_provider": "openai",
"mode": "chat",
"max_output_tokens": capacity,
"input_cost_per_token": 0,
"output_cost_per_token": 0.001,
}
}
)
deployment: Final = Deployment(litellm_params=DeploymentParams(model="openai/lens-capacity-test"))
assert output_tokens(deployment) == capacity
assert quote((deployment,), "Review") == pytest.approx(capacity * 0.001)
def test_explicit_deployment_output_setting_is_respected() -> None:
from litellm.proxy.lens.inference import output_tokens
deployment: Final = Deployment(litellm_params=DeploymentParams(model="custom/model", max_tokens=32000))
assert output_tokens(deployment) == 32000
def test_shared_context_capacity_leaves_room_for_the_entire_prompt(monkeypatch: pytest.MonkeyPatch) -> None:
from litellm.proxy.lens.inference import output_tokens
monkeypatch.setattr(litellm, "model_cost", {**litellm.model_cost})
litellm.register_model(
model_cost={
"openai/lens-shared-context": {
"litellm_provider": "openai",
"mode": "chat",
"max_output_tokens": 8192,
"max_input_tokens": 8192,
"input_cost_per_token": 0,
"output_cost_per_token": 0.001,
}
}
)
deployment: Final = Deployment(litellm_params=DeploymentParams(model="openai/lens-shared-context"))
short: Final = output_tokens(deployment, "Review this trace")
long: Final = output_tokens(deployment, "Review this trace " * 500)
assert 0 < long < short < output_tokens(deployment)
assert quote((deployment,), "Review this trace " * 500) == pytest.approx(long * 0.001)
def test_unknown_model_capacity_requires_explicit_operator_metadata() -> None:
from litellm.proxy.lens.inference import ModelCapacity, output_tokens
params: Final = DeploymentParams(model="openai/lens-unknown-capacity")
with pytest.raises(HTTPException) as error:
output_tokens(Deployment(litellm_params=params))
assert error.value.status_code == 400
assert "model_info.max_output_tokens" in error.value.detail
configured: Final = Deployment(litellm_params=params, model_info=ModelCapacity(max_output_tokens=32000))
assert output_tokens(configured) == 32000
def test_a_model_step_records_the_serving_model_and_its_tokens() -> None:
response: Final = ModelResponse(model="gpt-5.6", usage={"prompt_tokens": 1200, "completion_tokens": 80})
step: Final = model_step(response, ModelRequest(prompt="review", purpose="extract"), "analysis", 0.02)
assert (step.model, step.prompt_tokens, step.completion_tokens, step.cost) == ("gpt-5.6", 1200, 80, 0.02)
def test_a_response_without_usage_still_records_a_step_instead_of_failing_settlement() -> None:
response: Final = ModelResponse(model="gpt-5.6")
unpriced: Final = response.model_copy(update={"usage": None})
step: Final = model_step(unpriced, ModelRequest(prompt="review", purpose="cluster"), "analysis", 0.0)
assert (step.prompt_tokens, step.completion_tokens) == (0, 0)
assert step.label == "Compared observations"