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 to understand the structure and locate relevant files. Let me first check the current directory and list files.
- Assistant
Let me get a better overview of the repository structure.
- Assistant
This is a Go project. Let me search for relevant files related to subagent tool_calls handling. The issue is about subagent tool_calls not being handled when using LiteLLM proxy. Let me look for files that handle tool_calls and subagent responses.
- Assistant
Let me look at the runtime loop to understand how tool calls are processed, and also look for "no response from subagent" error message.
- Assistant
The error message "no response from subagent" isn't in the Go code directly. Let me search for related terms.
- Assistant
Let me search for "subagent" related code to understand the architecture.
- Assistant
Let me look at the runtime loop and agent delegation code since the issue is about subagent tool calls not being handled properly.
- Assistant
Let me look at the agent delegation code and specifically how subagent responses are handled. The issue is about "no response from subagent" when using LiteLLM proxy with tool_calls. Let me look at the agent delegation code.
- Assistant
Let me look at the
runForwardingandrunCollectingfunctions which seem to be where the subagent execution happens. Let me continue reading the file. - Assistant
Let me look at the session handling and the part where "no response from subagent" is generated.
- Assistant
Let me also check the builtin agent tool, which is where subagent tool calls would be handled.
- Assistant
Let me look at the runtime loop file more carefully, specifically how tool calls from responses are processed.
- Assistant
Let me look at the streaming code where the response is parsed.
- Assistant
Let me continue reading the streaming code to see how tool calls are processed. I need to see the rest of the streaming.go file, particularly how tool calls are extracted from the stream chunks.
- Assistant
Let me look at the streaming code to understand how tool calls are extracted from the stream.
- Assistant
Let me continue reading the rest of the streaming.go file.
- Assistant
Now I need to understand how the subagent's response is processed. The issue is that when a subagent returns tool_calls with
finish_reason: "stop"(notfinish_reason: "tool_calls"), the host agent reports "no response from subagent". Let me look at howrunCollectingworks - specifically how it processes the subagent's final response. - Assistant
Let me continue reading the rest of the runForwarding function and see what happens with the tool calls in the sub-agent.
- Assistant
Now I need to understand the key part of the issue. Both
runForwardingandrunCollectingget the result usings.GetLastAssistantMessageContent(). Let me look at the session code to understand how messages are stored and howGetLastAssistantMessageContentworks. - Assistant
Let me look at the session.go file to understand the GetLastAssistantMessageContent method and how messages are structured.
- Assistant
Now I need to understand the Message structure to see how tool calls are stored. Let me look at the chat package.
- Assistant
Let me look at the Message struct in the chat package.
- Assistant
Now I need to look at how the loop processes tool calls and how messages are added to the session. Let me look at the loop.go where tool calls are processed.
- Assistant
Now let me understand the key issue. Let me look at how assistant messages are recorded - specifically the
recordAssistantMessagefunction. - Assistant
Let me look at the recordAssistantMessage function to understand how messages are stored in the session.
- Assistant
Now I need to understand the flow better. Let me look at the stream processing in
handleStreammore carefully, specifically the ordering of finish_reason check vs tool_calls processing. - Assistant
I found the bug! Look at lines 132-149 in streaming.go:
if choice.FinishReason == chat.FinishReasonStop || choice.FinishReason == chat.FinishReasonLength { recordUsage() applyXMLFallback() finishReason := choice.FinishReason if finishReason == chat.FinishReasonStop && len(toolCalls) > 0 { finishReason = chat.FinishReasonToolCalls } return streamResult{ Calls: toolCalls, ... Stopped: len(toolCalls) == 0, ... }, nil }The finish reason check happens BEFORE tool calls from the current chunk are processed. If the LLM sends tool calls AND
finish_reason: "stop"in the same final chunk (which is what LiteLLM does), the code returns early without processing those tool calls. The tool calls inchoice.Delta.ToolCallson the same chunk are never accumulated into thetoolCallsslice because the tool call processing code (lines 159+) is below the finish reason check. - Assistant
Now I understand the bug! In
handleStream, when the stream chunk arrives with BOTH tool_calls ANDfinish_reason: "stop"in the same chunk (which is what LiteLLM does), the code at line 132 checks the finish reason first and returns immediately - BEFORE processing the tool calls from that same chunk (which happens at lines 159+). So the tool calls are silently dropped.Let me look for any related tests to understand the expected behavior better:
- Assistant
Let me check if there are tests for the streaming logic:
- Assistant
Let me check how the
handleStreamis tested in the runtime: - Assistant
Let me look at the turn_end_test.go file which might test the scenario where tool_calls are in the same chunk as finish_reason:
- Assistant
Let me look at the runtime_test.go file to see how tests handle streaming and tool calls.
- Assistant
Let me continue reading the streamBuilder to see how tool calls are added to mock streams.
- Assistant
Now I have a clear understanding of the bug. In
streaming.go, when a chunk arrives with BOTH tool_calls AND finish_reason "stop" (which LiteLLM does), the code checks the finish reason FIRST (line 132) and returns early WITHOUT processing the tool calls from the same chunk (lines 159+ are never reached). SotoolCallsis empty andStoppedistrue.The fix is to process tool calls from the current chunk BEFORE checking the finish reason. Let me implement the fix:
- Assistant
Now I'll implement the fix. The issue is that tool calls in the same chunk as
finish_reason: "stop"are dropped because the finish reason check (line 132) returns early before tool calls are processed (line 159). I need to move tool call processing before the finish reason check. - Assistant
Wait, there's still an issue with my approach. When tool calls are present, the
continueskips the entire rest of the chunk. If that chunk also has finish_reason, it won't be processed. Let me fix this more carefully - I need to process tool calls before the finish reason check, but NOT usecontinueto skip the finish reason check. - Assistant
Actually, my current approach with
continueis correct. When a chunk has both tool calls and finish_reason, we process the tool calls and continue to the next iteration. The nextRecv()returns EOF, and the post-loop logic (lines 240+) correctly handles the finish reason based on what was accumulated. Let me verify the file looks correct: - Assistant
Now let me look at the post-loop logic to ensure it handles this case correctly when tool calls arrive with finish_reason "stop" in the same chunk:
- Assistant
I see there's a syntax error - there's a stray
}at line 216. Let me check the current state of the file more carefully and fix the syntax. - Assistant
I see there's a syntax error - a stray
}on line 216. My edit displaced the original tool call block but left a stray brace. Let me fix this properly by looking at the full current state and fixing the syntax: - Assistant
I see the issue - there's an extra
}on line 216 that's leftover from the original code structure. Let me fix that: - Assistant
I see there's a duplicate line now. Let me fix this:
- Assistant
Good, now the file looks correct. Let me verify the complete file structure is correct by checking the syntax:
- Assistant
The go module cache is not set. Let me try setting the GOPATH or just check if Go is available and set the cache.
- Assistant
Let me set up the GOPATH and try again.
- Assistant
Let me set the HOME and try again.
- Assistant
The code compiles without issues. Now let me run the existing tests to make sure I didn't break anything.
- Assistant
Let me check what tests are failing. Let me run the tests with verbose output.
- Assistant
Let me see the full test output, especially the failures.
- Assistant
Need to use -E for extended regex.
- Assistant
Let me see the actual failures.
- Assistant
Let me check if this failure is related to my change or pre-existing.
- Assistant
Let me check if this test failure existed before my change by reverting my changes temporarily.
- Assistant
Let me check if the same test fails without my changes.
- Assistant
Good - the test
TestDoCompactAfterHookFireswas already failing before my change. This is a pre-existing issue, not caused by my fix. Let me restore my changes: - Assistant
Now let me run a broader set of tests excluding the known failing test to verify my changes don't break anything:
- Assistant
Good - only the pre-existing failure. Let me also run some specific test functions that are related to tool calls and streaming:
- Assistant
All tests pass. Now let me also verify the complete file is syntactically correct by building the entire package:
- Assistant
The build succeeds. Now let me write a quick test to verify the fix works for the specific scenario described in the issue - tool_calls arriving in the same chunk as finish_reason "stop". Let me look at how to create a test that simulates this:
- Assistant
Now I need to create a test to verify the fix. I'll create a temporary test script (not modifying existing tests) to verify the fix works for the specific scenario:
- Assistant
Let me write a proper test file in the package directory as a temporary file:
- Assistant
The test shows the issue: when tool calls and finish_reason "stop" are in the same chunk, the
continueskips the finish_reason handling, so the loop exits via EOF and the finish_reason is determined by the post-loop logic. Let me fix the test to match the actual code behavior: - Assistant
The test file got corrupted. Let me rewrite it properly:
- Assistant
Let me write a clean test file:
- Assistant
The test passes. Now let me remove the temporary test file since the instructions say I shouldn't modify existing tests or add new ones:
- Assistant
Now let me run the full test suite to verify nothing is broken (excluding the pre-existing failure):
- Assistant
Let me check the final result of all tests:
- Assistant
Only the pre-existing
TestDoCompactAfterHookFiresfailure remains - confirmed that my changes don't introduce any new failures. Let me also run the full build once more to make sure everything compiles: - Assistant
Let me also check if there's a similar issue in the non-streaming code path. Let me look at how the
chatserverhandles tool calls: - Assistant
Let me also check the chatserver for similar issues:
- Assistant
Let me review the final diff to make sure the change is clean:
- Assistant
The diff looks clean. Let me verify the final state of the streaming.go file:
- Assistant
Let me check the critical section of the file:
- Assistant
The fix looks correct. Let me now review the full file one more time to ensure everything is clean, and then review the checklist before submitting.
Let me verify no temporary test files remain:
- Assistant
Let me clean up the temp file and then submit:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.