fix(spend): keep the outer request as parent for nested interception follow-ups

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
mateo 2026-11-03 02:21:37 +00:00
parent dff64ad609
commit d52ec7f459
4 changed files with 155 additions and 5 deletions

View file

@ -8,7 +8,7 @@ from typing import Final
from pydantic import TypeAdapter, ValidationError
from litellm._internal_context import internal_sub_call
from litellm._internal_context import internal_parent_request_id, internal_sub_call
from litellm._logging import verbose_logger
from litellm.integrations.custom_logger import CustomLogger
from litellm.litellm_core_utils.agentic_followup_kwargs import build_agentic_followup_kwargs
@ -205,7 +205,10 @@ async def _execute_chat_completion_agentic_plan(
)
try:
with internal_sub_call(logging_obj.litellm_call_id if isinstance(logging_obj, LiteLLMLoggingObject) else None):
with internal_sub_call(
internal_parent_request_id.get()
or (logging_obj.litellm_call_id if isinstance(logging_obj, LiteLLMLoggingObject) else None)
):
response_followup = await litellm.acompletion(
model=full_model_name,
messages=patch.messages,

View file

@ -37,7 +37,7 @@ import litellm
import litellm.litellm_core_utils
import litellm.types
import litellm.types.utils
from litellm._internal_context import internal_sub_call
from litellm._internal_context import internal_parent_request_id, internal_sub_call
from litellm._logging import redact_string, verbose_logger
from litellm.anthropic_beta_headers_manager import aload_beta_headers_config, update_headers_with_filtered_beta
from litellm.constants import MAX_FILE_LIST_LIMIT, REALTIME_WEBSOCKET_MAX_MESSAGE_SIZE_BYTES
@ -6227,7 +6227,7 @@ class BaseLLMHTTPHandler:
)
try:
with internal_sub_call(logging_obj.litellm_call_id):
with internal_sub_call(internal_parent_request_id.get() or logging_obj.litellm_call_id):
response: ResponsesAPIResponse | BaseResponsesAPIStreamingIterator = await litellm.aresponses(
model=patch.model or model,
input=patch.messages, # pyright: ignore[reportArgumentType] # pre-existing mismatch surfaced by the Router import; patch messages are valid response input at runtime
@ -6362,7 +6362,9 @@ class BaseLLMHTTPHandler:
fingerprint=fingerprint,
)
with internal_sub_call(logging_obj.litellm_call_id if logging_obj is not None else None):
with internal_sub_call(
internal_parent_request_id.get() or (logging_obj.litellm_call_id if logging_obj is not None else None)
):
response: Final = await litellm.acompletion(
model=full_model_name,
messages=patch.messages,

View file

@ -33,6 +33,7 @@ from litellm.integrations.code_interpreter_interception.handler import (
)
from litellm.integrations.custom_logger import CustomLogger
from litellm.litellm_core_utils.chat_completion_agentic_loop import (
_execute_chat_completion_agentic_plan,
maybe_run_chat_completion_agentic_loop,
)
from litellm.litellm_core_utils.litellm_logging import Logging
@ -364,6 +365,60 @@ async def test_dispatcher_runs_followup_with_incremented_depth_and_patched_messa
assert logger.cleanup_calls == 1
@pytest.mark.parametrize(
("outer_parent", "expected_parent"),
(
pytest.param(None, "agentic-call-id", id="without-outer-parent"),
pytest.param("outer-id", "outer-id", id="preserves-outer-parent"),
),
)
@pytest.mark.asyncio
async def test_agentic_followup_preserves_outer_internal_parent(
outer_parent: str | None, expected_parent: str
) -> None:
from contextlib import nullcontext
from litellm._internal_context import internal_parent_request_id, internal_sub_call, is_internal_call
observed_context: Final = MagicMock()
async def fake_acompletion(**kwargs: object) -> str:
observed_context(internal_parent_request_id.get(), is_internal_call.get())
return "followup-response"
logging_obj: Final = Logging(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "what is 6*7?"}],
stream=False,
call_type="acompletion",
start_time=datetime.datetime(2026, 1, 1, tzinfo=datetime.timezone.utc),
litellm_call_id="agentic-call-id",
function_id="agentic-function-id",
)
outer_context: Final = internal_sub_call(outer_parent) if outer_parent is not None else nullcontext()
with patch.object(litellm, "acompletion", fake_acompletion):
with outer_context:
response: Final = await _execute_chat_completion_agentic_plan(
plan=AgenticLoopPlan(
run_agentic_loop=True,
request_patch=AgenticLoopRequestPatch(messages=_patched_messages()),
),
callback=CustomLogger(),
model="gpt-4o-mini",
optional_params={},
kwargs={},
logging_obj=logging_obj,
custom_llm_provider="openai",
depth=0,
max_loops=3,
fingerprints=[],
fingerprint="fp",
)
assert response == "followup-response"
observed_context.assert_called_once_with(expected_parent, True)
@pytest.mark.asyncio
@pytest.mark.parametrize(
"model,custom_llm_provider,routed_model",

View file

@ -5384,6 +5384,96 @@ async def test_chat_completion_agentic_followup_does_not_repeat_request_params_f
assert followup_calls[0]["model"] == "openai/gpt-5"
@pytest.mark.parametrize(
("outer_parent", "expected_parent"),
(
pytest.param(None, "responses-call-id", id="without-outer-parent"),
pytest.param("outer-id", "outer-id", id="preserves-outer-parent"),
),
)
@pytest.mark.asyncio
async def test_responses_agentic_followup_preserves_outer_internal_parent(
outer_parent: str | None, expected_parent: str
) -> None:
from contextlib import nullcontext
from litellm._internal_context import internal_parent_request_id, internal_sub_call, is_internal_call
from litellm.types.integrations.custom_logger import AgenticLoopPlan, AgenticLoopRequestPatch
observed_context: Final = Mock()
async def fake_aresponses(**kwargs: object) -> str:
observed_context(internal_parent_request_id.get(), is_internal_call.get())
return "followup-response"
outer_context: Final = internal_sub_call(outer_parent) if outer_parent is not None else nullcontext()
with patch.object(litellm, "aresponses", fake_aresponses):
with outer_context:
response: Final = await BaseLLMHTTPHandler()._execute_responses_agentic_plan(
plan=AgenticLoopPlan(
run_agentic_loop=True,
request_patch=AgenticLoopRequestPatch(messages=[{"role": "user", "content": "x"}]),
),
model="gpt-5",
response_api_optional_request_params={},
logging_obj=Mock(litellm_call_id="responses-call-id"),
kwargs={},
depth=0,
max_loops=3,
fingerprints=[],
fingerprint="fp",
)
assert response == "followup-response"
observed_context.assert_called_once_with(expected_parent, True)
@pytest.mark.parametrize(
("outer_parent", "expected_parent"),
(
pytest.param(None, "chat-call-id", id="without-outer-parent"),
pytest.param("outer-id", "outer-id", id="preserves-outer-parent"),
),
)
@pytest.mark.asyncio
async def test_chat_agentic_plan_followup_preserves_outer_internal_parent(
outer_parent: str | None, expected_parent: str
) -> None:
from contextlib import nullcontext
from litellm._internal_context import internal_parent_request_id, internal_sub_call, is_internal_call
from litellm.types.integrations.custom_logger import AgenticLoopPlan, AgenticLoopRequestPatch
observed_context: Final = Mock()
async def fake_acompletion(**kwargs: object) -> str:
observed_context(internal_parent_request_id.get(), is_internal_call.get())
return "followup-response"
outer_context: Final = internal_sub_call(outer_parent) if outer_parent is not None else nullcontext()
with patch.object(litellm, "acompletion", fake_acompletion):
with outer_context:
response: Final = await BaseLLMHTTPHandler()._execute_chat_completion_agentic_plan(
plan=AgenticLoopPlan(
run_agentic_loop=True,
request_patch=AgenticLoopRequestPatch(messages=[{"role": "user", "content": "x"}]),
),
model="gpt-5",
messages=[{"role": "user", "content": "x"}],
optional_params={},
kwargs={},
custom_llm_provider="openai",
depth=0,
max_loops=3,
fingerprints=[],
fingerprint="fp",
logging_obj=Mock(litellm_call_id="chat-call-id"),
)
assert response == "followup-response"
observed_context.assert_called_once_with(expected_parent, True)
_UPSTREAM_HEADERS: Final = {"x-request-id": "req_upstream", "x-ratelimit-remaining-requests": "41"}