ISSUE DESCRIPTION Subagent toolcalls not handled when using LiteLLM proxy Summary When…
- User
ISSUE DESCRIPTION
Subagent tool_calls not handled when using LiteLLM proxy
Summary
When using cagent with a LiteLLM proxy (OpenAI-compatible API), a subagent can return a valid
tool_callsresponse, but the host agent reports “no response from subagent” and does not execute the tool call.Environment
- cagent: v1.19.7
- OS: Linux (WSL2) x86_64
- LLM: gemini-pro via LiteLLM proxy (OpenAI-compatible)
Expected
Host agent receives the subagent response and executes the tool call(s), then continues the conversation with the tool result.
Actual
Host agent reports there is “no response from subagent” even though the subagent response contains a valid
tool_callsentry. The requested tool call is not executed.Minimal reproduction
- Configure cagent to use LiteLLM as the model provider (OpenAI-compatible API).
- Run a workflow where the subagent is asked to use tools.
- The subagent returns a tool call (e.g.,
company_searchwith JSON arguments) and finishes withfinish_reason: "stop". - The host agent logs “no response from subagent” and does not invoke the tool.
Notes
- The subagent response includes a valid tool call object (
type: function,name: company_search,arguments: {"datasources": ["confluence", "github", "gdrive"], "query": "LiteLLM usage teams integrations standards"}). - I can reproduce consistently with LiteLLM as the proxy in front of an OpenAI-compatible endpoint.
Please let me know if you want a sanitized example payload or a minimal agent config.
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto get the changes that need to be submitted. Assistant
I'll start by exploring the repository structure to understand the codebase and locate the relevant code for handling subagent tool_calls.
Assistant
The repository doesn't seem to have many Python files. Let me check what type of project this is.
Assistant
This is a Go project. Let me explore the structure more, particularly looking for subagent-related code.
Assistant
Let me look at the agent delegation related files since the issue is about subagent tool calls.
Assistant
Let me look for where tool calls are handled, especially the part that processes responses from the LLM and looks for tool calls:
Assistant
Let me search more broadly for where subagent responses are processed:
Assistant
Now let me look at the
runForwardingandrunCollectingfunctions more closely. The issue mentions that the subagent returns tool_calls but the host agent reports "no response from subagent". Let me check whereGetLastAssistantMessageContent()is used:Assistant
Let me examine the session.go file to understand how
GetLastAssistantMessageContent()works:Assistant
Now I see the issue! The
getLastMessageContentByRolefunction only looks atmessage.Message.Contentwhich is the text content of the message. When a subagent returns atool_callsresponse (without any text content), this function will return an empty string, even though there's a valid tool call.Let me look at how the session handles tool calls in messages:
Assistant
Now let me look at the runtime's loop_steps.go or wherever tool calls are processed from the LLM response:
Assistant
Let me examine the loop_steps.go file which likely handles the tool call processing:
Assistant
Let me look at the main loop.go file where tool calls are processed:
Assistant
Let me look at the full loop.go to understand the flow better:
Assistant
Let me look at where the
res(stream response) is built and how it handles tool calls:Assistant
The file is too large. Let me continue investigating the code flow to understand how tool calls are handled.
Assistant
...
Assistant
Now I understand the stream processing. The
handleStreamfunction collectstoolCallsfrom the stream and returns them instreamResult. Let me check where the tool calls get processed and how the message gets stored:Assistant
Now I need to understand where the issue occurs. Let me look at how the assistant message is recorded and how the tool calls are added to the session:
Assistant
Let me look at the
recordAssistantMessagefunction:Assistant
Now I understand the flow. The assistant message with tool calls is properly recorded via
recordAssistantMessage. Let me look at theGetLastAssistantMessageContentfunction more closely to understand how it handles messages with tool calls:Assistant
Now I can see the problem. The
getLastMessageContentByRolefunction only returns theContentfield of the message. When a subagent returns a response withtool_callsbut no textContent, this function returns an empty string.The issue is in
agent_delegation.gowhererunForwardingreturns:return tools.ResultSuccess(s.GetLastAssistantMessageContent()), nilIf the subagent's last message only contains tool calls (no text content),
GetLastAssistantMessageContent()returns an empty string, which makes the host agent think there's "no response from subagent".Let me verify this by looking at the test file for agent delegation:
Assistant
Now I understand the issue clearly. Let me trace through what happens when a subagent makes a tool call:
- The subagent runs via
RunStream(inrunForwarding) - The subagent's response contains
tool_callsbut possibly no textContent - The tool calls are executed by
processToolCalls - After all events are processed,
runForwardingreturnstools.ResultSuccess(s.GetLastAssistantMessageContent()) GetLastAssistantMessageContent()only returns the text content, not the tool call results
Let me look at what happens after the tool call is executed to understand how the tool result is stored:
- The subagent runs via
Assistant
Let me see what happens when a tool result is recorded - particularly if a new message is added to the session:
Assistant
Now let me check how the tool response messages are stored in the session:
Assistant
Now I understand the flow. When a tool call is executed, a tool response message is added to the session with
Role: chat.MessageRoleTool. The issue is thatGetLastAssistantMessageContent()only looks for messages withRole == chat.MessageRoleAssistant, so it will skip the tool response messages and look for the last assistant message.Let me trace the issue more carefully. When the subagent returns a tool call:
- The assistant message (with tool_calls) is added to the session via
recordAssistantMessage - The tool call is executed and the tool result is added to the session
- After the loop completes,
runForwardingreturnss.GetLastAssistantMessageContent() - This looks for the last message with
Role == chat.MessageRoleAssistant - If the assistant message only had tool calls (no content), it returns an empty string
So the issue is that when an assistant message has tool calls but no text content,
GetLastAssistantMessageContent()returns an empty string. This is the root cause.Let me look at what the expected behavior should be. The
runForwardingfunction should return a meaningful result even when the subagent's response is tool calls. Let me check what should be returned in this case:- The assistant message (with tool_calls) is added to the session via
Assistant
Now I see the problem clearly. The
runForwardingfunction returnstools.ResultSuccess(s.GetLastAssistantMessageContent()), but this only returns the text content of the last assistant message. When the subagent's response contains tool calls that have been executed, the tool call results are in the session butGetLastAssistantMessageContent()doesn't include them.Let me look at how the session stores messages and what information is available after the subagent completes:
Assistant
Let me understand the complete picture by checking what messages are in a session after a tool call has been executed:
Assistant
Now let me check the
chat.Messagestructure to understand how tool calls and tool results are stored:Assistant
count: 5
Assistant
...
Assistant
...
Assistant
Let me look at the loop logic to see how it decides whether to continue after receiving tool calls:
Assistant
Let me check what the
streamResult.Stoppedfield means and how it controls the loop continuation:Assistant
So
Stoppedisfalsewhen there are tool calls to execute. Let me check the loop to see how it continues:Assistant
...
Assistant
Now I understand the flow better. Let me re-read the issue description carefully. The issue says the subagent returns tool calls with
finish_reason: "stop". Let me check how this is handled in the streaming code:Assistant
Let me look at what happens when the stream ends (EOF path):
Assistant
I see the issue now! Look at line 279:
Stopped: stoppedDueToNoOutput. This is set totrueif there's no content AND no tool calls. But the issue is that when there ARE tool calls but no text content,stoppedDueToNoOutputwould befalse, so the stream should continue.Let me trace through the issue more carefully. The problem description says:
- The subagent returns a tool call with
finish_reason: "stop" - The host agent reports "no response from subagent"
This suggests that when LiteLLM proxy returns the response, it might not be sending the tool calls in the streaming delta format, but rather in the final choice. Let me check if there's another code path:
- The subagent returns a tool call with
Assistant
Thinkable - current file content with line numbers for /docker-agent/pkg/runtime/streaming.go (293 lines loaded) Let me show you more context around how choices are processed...
Assistant
I see, I need to use the proper tool call format. Let me continue investigating the streaming code to understand how it handles non-streaming responses or responses where tool calls might come in a different format.
Assistant
Let me look at how the chat.MessageStream and chat.Choice types are defined to understand how the response is parsed. I need to check if there's a case where tool calls come in a non-delta format (like in non-streaming responses).
Assistant
Let me look at the MessageDelta and MessageStreamChoice types to understand the structure.
Assistant
I found the issue! Looking at the
handleStreamfunction instreaming.go, the code checks forfinish_reasonbefore processing the tool call deltas. This means if a provider (like LiteLLM proxy) sends the tool calls in the same chunk as thefinish_reason: "stop", those tool calls will never be processed.Let me verify this by looking at the exact order of operations in the streaming code:
Assistant
Perfect! Now I can see the issue clearly! The problem is in the order of operations in the
handleStreamfunction:- Line 132-148: If
choice.FinishReason == chat.FinishReasonStoporchat.FinishReasonLength, it returns immediately without processing the tool calls inchoice.Delta.ToolCalls - Line 159+: Tool calls are only processed AFTER the finish reason check
This means if a LiteLLM proxy (or any provider) sends the tool calls in the same chunk as
finish_reason: "stop", the tool calls are never processed because the function returns early.The fix is to process the tool calls BEFORE checking for the finish reason and returning. Let me verify this is the issue and create a fix.
- Line 132-148: If
Assistant
Now let me create a test script to reproduce the issue and verify my understanding:
Assistant
Great! Now I understand the issue. Let me fix it. The solution is to process the tool calls BEFORE checking for the finish reason and returning early.
Assistant
Now I'll apply the fix. The solution is to process tool calls BEFORE checking for the finish reason:
Assistant
Let me apply the fix properly by rewriting the relevant section of the streaming.go file:
Assistant
Let me try to copy the new file to replace the old one:
Assistant
Good, the file has been copied. Now I need to verify that the tests still pass. Let me run the existing tests for the streaming.go file to make sure my changes don't break anything.
Assistant
Let me run all the tests in the runtime package to make sure nothing is broken:
Assistant
I see there's a FAIL at the end. Let me check what failed:
Assistant
Let me see more details about the TestDoCompactAfterHookFires failure:
Assistant
This test failure is unrelated to my changes - it's about compaction hooks. Let me check if this test was passing before my changes by reverting and running it:
Assistant
Let me check if the test was passing before my changes:
Assistant
Good! The test was already failing before my changes, so it's a pre-existing issue. Let me restore my changes and verify that my fix doesn't break any tests that were passing before:
Assistant
Assistant
Let me continue checking the test results to see if there are any new failures beyond the pre-existing one.
Assistant
Good! The only failing test is
TestDoCompactAfterHookFires, which was already failing before my changes. This confirms that my fix doesn't break any existing tests.Now let me verify my fix more thoroughly by examining the diff to make sure the changes are correct:
Assistant
The diff looks correct and clean. My fix:
-
Moves tool call processing BEFORE the finish reason check - This ensures that when a provider (like LiteLLM proxy) sends tool_calls in the same chunk as finish_reason, the tool calls are processed before we return early.
-
Adds clear comments explaining why we process tool calls first - This helps future maintainers understand the rationale.
-
Maintains original behavior for other cases - The "continue" statement after processing tool calls ensures we skip content processing for chunks that only have tool calls.
The fix is minimal, focused, and addresses the root cause of the issue. Let me verify this fix one more time by looking at the current state of the file:
-
Assistant
Perfect! The fix looks good. The code now:
- Processes tool calls first (lines 132-193)
- Tracks the provider finish reason (lines 195-199)
- Checks for termination conditions AFTER processing tool calls (lines 201-218)
- Skips content processing if we have tool calls (lines 220-223)
- Processes content and reasoning for chunks without tool calls (lines 225+)
This ensures that tool calls in the same chunk as finish_reason are properly processed before returning early.
Now let me create a simple test to verify my fix works correctly. I'll create a temporary test script that simulates the scenario described in the issue.
Assistant
Now let me go through the checklist from the issue description and verify everything:
Assistant
Let me verify my fix one more time by looking at the complete diff and ensuring it's correct:
Assistant
Perfect! I've successfully fixed the issue. Let me provide a final summary of what I did:
Summary
Root Cause
The issue was in
/docker-agent/pkg/runtime/streaming.goin thehandleStream()function. The code was checking forfinish_reason("stop" or "length") BEFORE processing tool calls. When LiteLLM proxy (or other OpenAI-compatible proxies) sends tool_calls in the same streaming chunk asfinish_reason="stop", the function would return early without processing those tool calls.The Fix
I reordered the code to:
- Process tool calls FIRST (if present in the chunk)
- Track the provider finish reason
- Check for termination conditions (stop/length) AFTER processing tool calls
- Return with the processed tool calls included in the result
This ensures that tool calls are always processed, regardless of whether they arrive in the same chunk as the finish_reason.
Verification
- ✓ Successfully reproduced the issue by analyzing the code flow
Assistant
Perfect! Let me submit the fix now.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.