pruneMessages

Drop old tool traffic and reasoning from a history.

A pure function that deletes old tool calls, tool results, and (for UIMessage histories) reasoning from a conversation. No model call, no cost.

Pruning is lossy by design. When the goal, the decisions, or the failed approaches need to survive, use compaction: instead, which summarizes rather than deletes.

let trimmed = pruneMessages(
  messages,
  toolCalls: .beforeLastMessages(6, tools: ["search"])
)

Signature

func pruneMessages(
    _ messages: [UIMessage],
    reasoning: PruneScope = .none,
    toolCalls: [PruneToolCalls] = [],
    emptyMessages: PruneEmptyMessages = .remove
) -> [UIMessage]

Defined in Sources/AI/Core/PruneMessages.swift.

Parameters

ParameterTypeDefault
_[UIMessage]required
reasoningPruneScope.none
toolCalls[PruneToolCalls][]
emptyMessagesPruneEmptyMessages.remove

Overloads

3 other forms of this function exist:

func pruneMessages(
    _ messages: [Message],
    toolCalls: PruneToolCalls,
    emptyMessages: PruneEmptyMessages = .remove
) -> [Message]
func pruneMessages(
    _ messages: [Message],
    toolCalls: [PruneToolCalls] = [],
    emptyMessages: PruneEmptyMessages = .remove
) -> [Message]
func pruneMessages(
    _ messages: [UIMessage],
    reasoning: PruneScope = .none,
    toolCalls: PruneToolCalls,
    emptyMessages: PruneEmptyMessages = .remove
) -> [UIMessage]

Returns

A new array of the same type, with the pruned entries removed. Dropping a tool call also drops its result and any approval response.

See also