Contents

onToolCall(perform:)

Runs an action whenever a tool is called within this dynamic profile.

Declaration

func onToolCall(perform action: nonisolated(nonsending) sending @escaping () async throws -> Void) -> some LanguageModelSession.DynamicProfile

Mentioned in

Discussion

When the onToolCall closure throws an error, the caller’s respond or response will propagate that error.

Use this to track or log tool usage within a dynamic profile:

struct MyDynamicProfile: LanguageModelSession.DynamicProfile {
  var body: some LanguageModelSession.DynamicProfile {
    Profile {
      Instructions("You are a helpful assistant.")
      SomeTool()
    }
    .onToolCall {
      toolCallCount += 1
    }
  }
}

See Also

Observing life cycle modifiers