removeOrphanedToolCalls

fun removeOrphanedToolCalls(messages: List<Message>): List<Message>

Removes Tool.Call messages that have no matching Tool.Result in the message list.

Orphaned tool calls appear when the agent strategy does not route a tool call through the standard execution loop (e.g. using it as a graph-terminating signal without recording the result in the prompt). Properly implemented agents should ensure every tool call produces a corresponding Tool.Result, but older training artifacts or incorrectly wired strategies may still contain orphans.

LLM providers (OpenAI, etc.) require every tool_call_id to have a corresponding tool response message, so orphaned calls must be stripped before sending.