---
title: "onPrompt(perform:)"
framework: foundationmodels
role: symbol
role_heading: Instance Method
path: "foundationmodels/languagemodelsession/dynamicprofile/onprompt(perform:)"
---

# onPrompt(perform:)

Runs an action before the model is invoked for this dynamic profile.

## Declaration

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

```

## Mentioned in

Composing dynamic sessions with instructions and profiles

## Discussion

Discussion When the onPrompt closure throws an error, the caller’s respond or response will propagate that error. Use this to observe or log prompts before generation begins: struct MyDynamicProfile: LanguageModelSession.DynamicProfile {   var body: some LanguageModelSession.DynamicProfile {     Profile {       Instructions("You are a helpful assistant.")     }     .onPrompt {       promptCount += 1     }   } }

## See Also

### Observing life cycle modifiers

- [onActivate(perform:)](foundationmodels/languagemodelsession/dynamicprofile/onactivate(perform:).md)
- [onDeactivate(perform:)](foundationmodels/languagemodelsession/dynamicprofile/ondeactivate(perform:).md)
- [onResponse(perform:)](foundationmodels/languagemodelsession/dynamicprofile/onresponse(perform:).md)
- [onToolCall(perform:)](foundationmodels/languagemodelsession/dynamicprofile/ontoolcall(perform:).md)
- [onToolOutput(perform:)](foundationmodels/languagemodelsession/dynamicprofile/ontooloutput(perform:).md)
