Skip to content

Conversation

Arkitecht
Copy link
Contributor

Description

Adds fluent onSuccess method to add handlers for when the response is successfully received by the LLM. Sends the request and response as arguments. Can assign multiple handlers per-request.

For example:

$response = Prism::text()
            ->using(Provider::Anthropic, 'claude-3-5-sonnet-latest')
            ->onSuccess(function ($request, $response): void {
                //AiUsage is a model that stores token usage
                AiUsage::create([
                     'provider' => $request->provider(),
                     'model' => $request->model(),
                     'prompt_tokens' => $response->usage->promptTokens,
                     'completion_tokens' => $response->usage->completionTokens
                ]);
            })
            ->onSuccess(fn($request, $response) => dump('Success!'))
            ->withPrompt('Who are you?')
            ->asText();

@prism-php prism-php deleted a comment from shoemoney Oct 11, 2025
@sixlive
Copy link
Contributor

sixlive commented Oct 11, 2025

I LOVE THIS, as part of #601 I also added an onComplete handler system. So I'm going to close this in favor of that but this is a banger PR. Thank you so much!

@sixlive sixlive closed this Oct 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants