removeOrphanedToolCalls

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

Removes tool call parts that have no matching tool result in the message list, dropping the carrying message entirely if nothing else is left in it.

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.