Adding a Claude-powered feature to a Laravel app, properly
Structured outputs, streaming, tools, queues, real cost, caching, the Batch API and evals, built into a real invoicing app in six parts with every output shown and the bill at the end.
By Abhishek Fouzdar
Most tutorials on "adding AI to your app" end at the first API call. The hard part starts after it: what shape the reply should have, how to show it while it is being written, how to let the model read your data without reading the wrong tenant's, what it costs per feature and per customer, what happens at 2 am when the API is overloaded, and how you know the thing still works after a prompt change. This series answers those questions on a real app, in six parts, with every command run and every output shown, and with the bill at the end. Laravel 13, the official Anthropic PHP SDK 1.4, Claude Opus 5.5 and Haiku 5.5, on 3 November 2026.
The app is InvoiceMini, the invoicing app from the testing series: companies, clients, invoices with lines, payments through a signed webhook, PDFs from a queue, reminders on a schedule. It gets four features: an invoice drafted from a sentence, a payment reminder written while you watch, an assistant that answers questions from the company's own invoices, and a monthly summary written on the queue. Each feature exists to teach one part of the API properly.

Who this is for
You write Laravel, you have an API key, and you have either not added a model to an app yet or added one in a way you would not want to defend in a code review. Every API concept is explained the first time it appears, every part ends with what went wrong while writing it, and the testing is as serious as the feature: the whole suite runs without the network, and a separate eval measures the model itself.
What you will end up with
A public repository, shipbytes/laravel-claude-guide, with one commit per part, 172 tests that run in under thirty seconds without an API key, a 100% type-coverage gate, a twelve-case eval that runs against the real API for seven cents, and a usage table that puts a price on every call.
| Feature | What it teaches |
|---|---|
| Invoice from plain English | Structured outputs: a JSON schema derived from a PHP class, the API guaranteeing the shape, the company's data in the system prompt, a form that fills itself in for review. |
| Payment reminder, streamed | The SDK's event stream, Laravel's server-sent events response, an EventSource that never reconnects into a second reminder, cancellation that stops the bill. |
| Assistant over the account | Tools: three read-only lookups scoped to one company, the tool runner driving the loop, a saved history for follow-ups, markdown answers with HTML stripped. |
| Monthly summary, on the queue | Facts computed in PHP, token counting before the call, streaming on the server, a job with slow retries on top of the SDK's fast ones, the Batch API at half price. |
| The gate and the bill | Haiku in front of Opus, a claude_calls table with the four kinds of token the API bills, prompt caching read from the rows, php artisan claude:usage. |
| Knowing it works | A fake PSR-18 transport for the suite, an eval set with a deterministic grader, Opus against Haiku on the same cases, prompts frozen as snapshots, a nightly run with a budget. |
The parts
- The SDK, one client in the container, and the first call. Why the official SDK, a config file with the models and their prices in version control,
claude:pingwith a cost column, and a fake transport so tests never touch the network. - An invoice draft from plain English, through structured outputs. The draft as a PHP class, what the SDK derives from it, why a nullable property is not a nullable field, and a form prefilled through
old()input. - A payment reminder streamed into the page.
createStream()and the message accumulator,response()->eventStream(), the three-event protocol, Stop that stops, and tests that decode the stream back into the email. - An assistant that answers from your own data, through tools. What a tool is on the wire, three tools built on the company's relations, the tool runner, a step limit, the saved history, and the split between faking the model and keeping everything else real.
- Queues, retries, real cost, caching measured, the Batch API and Haiku. The usage table, cache-aware pricing, a Haiku classifier that turned away an injection attempt, the summary job, one batch for every company, and what caching did to four requests.
- An eval set, a grader, prompt snapshots and a nightly run. Twelve cases, a grader with named failures, Opus and Haiku on the same set three times over, a
maxTokensbug only the eval could find, and GitHub Actions with a dollar cap.
What it cost
Every real call made while writing the series went through the app's own usage table from part 5 onwards, and through log lines before that. The table at the end:
| feature | model | calls | input | cache read | output | cost |
| assistant | claude-opus-5-5 | 4 | 2328 | 2505 | 674 | $0.027468 |
| classify | claude-haiku-5-5 | 3 | 1158 | 0 | 137 | $0.000184 |
| draft | claude-haiku-5-5 | 12 | 13280 | 0 | 1279 | $0.001968 |
| draft | claude-opus-5-5 | 23 | 25463 | 0 | 2073 | $0.143312 |
| summary | claude-opus-5-5 | 2 | 692 | 0 | 1092 | $0.019216 |
| total | | 44 | 42921 | 2505 | 5255 | $0.192148 |
With the calls from parts 1 to 4 that predate the table, the whole series, including three full eval runs on Opus and two on Haiku, cost about a quarter of a dollar. The most expensive single thing in it is a twelve-case eval on Opus at seven cents, which is the right thing to be spending on.
What you need
- PHP 8.4 or later, Composer, Node 20 or later. The series used PHP 8.5.4, Laravel 13.38 and SDK 1.4.0.
- An Anthropic API key with a few dollars on it. Parts 1 to 5 cost cents to follow; part 6's evals cost seven cents a run on Opus.
- The InvoiceMini app from the testing series, or the repository above at its first commit, which is that app.
- An evening for parts 1 to 3 and another for 4 to 6.
Two rules that run through every part
Fake the model, keep everything else real. The feature tests script what Claude says and let the real database, the real tools, the real page and the real cost arithmetic run. That is what makes the suite fast, free and honest at the same time; the eval covers the part the suite cannot.
The app computes, the model writes. Overdue days, totals per currency, minor units, what is outstanding: PHP does all of it, and the prompt carries the answers. A model asked to add numbers will get them right most of the time, and most of the time is not the standard for an invoice.
New posts by email
Get new posts by email. Nothing else, no digests, unsubscribe in one click.
Comments
Comments are GitHub Discussions in shipbytes/discussions. Sign in with GitHub to post.
In this series
6 parts- OverviewAdding a Claude-powered feature to a Laravel app, properly
- Part 1The SDK, one client in the container, and the first call
- Part 2An invoice draft from plain English, through structured outputs
- Part 3A payment reminder streamed into the page
- Part 4An assistant that answers from your own data, through tools
- Part 5Queues, retries, real cost, caching measured, the Batch API and Haiku
- Part 6An eval set, a grader, prompt snapshots and a nightly run