Prism Workers AI — A Cloudflare Workers AI Provider for Prism PHP

Last updated on by

Prism Workers AI — A Cloudflare Workers AI Provider for Prism PHP image

Prism Workers AI is a Cloudflare Workers AI provider for Prism PHP and the Laravel AI SDK. It routes requests through Cloudflare's AI Gateway /compat endpoint.

  • Generate natural language text, structured outputs, and stream responses.
  • Create vector representations using Cloudflare embedding models.
  • Automate tool execution with support for complex, multi-step workflows.
  • Extract and utilize reasoning content from supported reasoning models.
  • Maintain conversation context using session affinity for prompt prefix caching.
  • Compatible with Prism PHP and Laravel AI SDK's agent() helper.

Text Generation and Streaming

Generate text using any Workers AI model through Prism's fluent API:

use Prism\Prism\Facades\Prism;
 
$response = Prism::text()
->using('workers-ai', 'workers-ai/@cf/meta/llama-3.3-70b-instruct-fp8-fast')
->withPrompt('Hello!')
->asText();

Streaming works the same way:

$stream = Prism::text()
->using('workers-ai', 'workers-ai/@cf/meta/llama-3.3-70b-instruct-fp8-fast')
->withPrompt('Tell me a story')
->asStream();

Embeddings

Unlike Prism's built-in xAI driver, this package supports embeddings:

$response = Prism::embeddings()
->using('workers-ai', 'workers-ai/@cf/baai/bge-large-en-v1.5')
->fromInput('Hello world')
->generate();

Tool Calling

The package supports tool calling with multi-step workflows. Workers AI requires content to always be present on assistant messages, even when empty during tool call responses — this package handles that automatically:

$response = Prism::text()
->using('workers-ai', 'workers-ai/@cf/meta/llama-3.3-70b-instruct-fp8-fast')
->withTools([$weatherTool])
->withMaxSteps(3)
->withPrompt('What is the weather?')
->asText();

Reasoning Models with Thinking Content

The package extracts reasoning content from reasoning models such as Kimi K2.5. The reasoning chain is available alongside the final response:

$response = Prism::text()
->using('workers-ai', 'workers-ai/@cf/moonshotai/kimi-k2.5')
->withMaxTokens(2000)
->withPrompt('What is 15 * 37?')
->asText();
 
$response->text; // "555"
$response->steps[0]->additionalContent['thinking'];
// "The user is asking for the product of 15 and 37..."

When streaming, you receive thinking events before the text output:

$stream = Prism::text()
->using('workers-ai', 'workers-ai/@cf/moonshotai/kimi-k2.5')
->withMaxTokens(2000)
->withPrompt('Explain briefly why the sky is blue.')
->asStream();
 
foreach ($stream as $event) {
// ThinkingStartEvent, ThinkingEvent (deltas), ThinkingCompleteEvent
// then TextStartEvent, TextDeltaEvent, TextCompleteEvent
}

Set withMaxTokens(2000) or higher when using reasoning models, since reasoning tokens count against the limit.

Laravel AI SDK Integration

If you're using laravel/ai, the package also registers a workers-ai driver:

use function Laravel\Ai\agent;
 
$response = agent(instructions: 'You are a helpful assistant.')
->prompt('Hello!', provider: 'workers-ai');

You can learn more about this package on GitHub at meirdick/prism-workers-ai.

Note: the package requires PHP 8.2+ and Prism PHP ^0.99, with optional support for Laravel AI SDK ^0.3.

Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Acquaint Softtech

Hire Laravel developers with AI expertise at $20/hr. Get started in 48 hours.

Visit Acquaint Softtech
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $9500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises

The latest

View all →
Showcase Your PhpStorm Expertise on LinkedIn image

Showcase Your PhpStorm Expertise on LinkedIn

Read article
Privacy Filter: Detect PII in Text from Laravel image

Privacy Filter: Detect PII in Text from Laravel

Read article
NationForge: A Self-Hosted Admin Panel for Civic Organizations image

NationForge: A Self-Hosted Admin Panel for Civic Organizations

Read article
Monitor Laravel Queues, Commands, and Schedulers on Any Driver with Vigilance image

Monitor Laravel Queues, Commands, and Schedulers on Any Driver with Vigilance

Read article
Watch the Teaser for 'The Story of PHP' Documentary image

Watch the Teaser for 'The Story of PHP' Documentary

Read article
Ship AI with Laravel: Give Your AI Agent Live Web Search image

Ship AI with Laravel: Give Your AI Agent Live Web Search

Read article