PHP code example of particle-academy / prism-harness
1. Go to this page and download the library: Download particle-academy/prism-harness library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
particle-academy / prism-harness example snippets
$session = PrismHarness::for($user)->session('support');
$session->usingMode('plan')->usingModel('claude-sonnet-4-5');
$session->lock(function (Session $session) {
// whatever must not happen twice
});
'stores' => [
'ephemeral' => 'redis', // recommended in production
'durable' => 'database',
],
'drivers' => [
'redis' => [
// An assertion about your infrastructure, not a preference.
'durable' => true,
],
],
use Prism\Harness\Models\Thread;
$thread = Thread::forParticipant($user, 'support');
$response = Prism::text()
->using(Provider::Anthropic, 'claude-sonnet-4-5')
->withThread($thread) // everything said so far
->withPrompt('And after that?') // the turn being taken now
->asText();
$thread->record($response->messages); // the full exchange, tool steps
// config/prism-harness.php — the shipped strategy, deterministic and no model call
'context' => ['keep_recent' => 200],
// or your own
class KeepWhatMatters implements Prism\Harness\Contracts\CompactionStrategy
{
public function compact(array $messages): CompactionOutcome
{
return new CompactionOutcome(kept: /* ... */, evicted: /* ... */);
}
}
$this->app->singleton(CompactionStrategy::class, KeepWhatMatters::class);
$this->app->bind(SummaryBudget::class, fn () => new TruncateTo);
// May this tool be OFFERED to this run at all?
Gate::define('harness.tool', fn ($user, Session $s, Tool $t) => $user->can('use', $t->name()));
// May THIS call proceed, with THESE arguments?
Gate::define('harness.tool.call', fn ($user, Session $s, Tool $t, array $args) =>
str_starts_with($args['path'] ?? '', '/tmp/'));
foreach ($session->stream('Refactor the billing job') as $event) {
// Prism's stream events, untouched — render them as you already do
}
// the turn is recorded and the run closed out when the stream ends
use Prism\Prism\Schema\{ArraySchema, ObjectSchema, StringSchema};
$schema = new ObjectSchema('plan', 'A proposed plan', [
new StringSchema('title', 'What the plan is called'),
new ArraySchema('steps', 'What to do', new StringSchema('step', 'One step')),
],
use Prism\Harness\Exceptions\StructuredSchemaViolation;
try {
$plan = $session->sendStructured($brief, $schema)->structured();
} catch (StructuredSchemaViolation $violation) {
$violation->code(); // structured_schema_violation, or structured_unreadable
$violation->problems(); // every way it missed, not the first
$violation->document(); // what the model actually said
}
use Prism\Prism\ValueObjects\Media\Image;
$session->send('What is wrong with this layout?', null, [
Image::fromBase64($request->string('screenshot'), 'image/png'),
]);
$reply = (new VoiceExchange)->exchange($session, Audio::fromBase64($base64, 'audio/webm'));
$reply->heard; // the transcript — what the model was actually asked
$reply->text; // the answer
$reply->audio; // the answer spoken, or null when the turn produced no prose
$reply->empty; // true when nothing was heard, and no turn was spent
use Prism\Harness\Contracts\CallCostObserver;
use Prism\Harness\Exceptions\CallRefused;
use Prism\Harness\Subagents\{CallReservation, CallSettlement, RunContext};
final class AccountCosts implements CallCostObserver
{
public function reserving(CallReservation $call, RunContext $run): void
{
// Atomically reserve $call->worstCaseUsd in your account, keyed by $call->callId.
// Throw new CallRefused('account_exhausted') if your account cannot admit it.
}
public function settled(CallSettlement $call, RunContext $run): void
{
// Replace that reservation with $call->chargedUsd, using the same callId.
}
}
$run = RunContext::root('build-'.$buildId, $budget, pricing: $prices,
observer: app(AccountCosts::class));
$session->send('Build this page', context: $run);
$tasks = $session->tasks();
$tasks->add('Draft the summary', 't-1');
$tasks->add('Check the figures', 't-2');
while ($tasks->pending() > 0) {
$task = $tasks->claim($workerId); // one atomic call
if ($task === null) {
break; // someone else holds what is left
}
$result = $session->send($task->instruction());
$tasks->release($task, $workerId, TaskOutcome::Done); // the APPLICATION decides
}
$session = PrismHarness::for($user)->session(); // rehydrated, not constructed
$session->mode('plan'); // persisted on the thread
$response = $session->send('Refactor the billing job');
if ($response->awaitingApproval()) {
$session->approve($response->pendingApprovals()[0]);
}
php artisan harness:doctor
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.