📚 Encyclopedia

AI & Claude · updated 2026-10-13

Structured JSON output from Claude with the Anthropic PHP SDK

anthropic-sdkphpstructured-outputjson-schemaprompt-cachingguzzle

The plumb web app (a quoting and invoicing tool for a solo plumber) turns a dictated end-of-day "debrief" and receipt photos into structured drafts with one Claude call each, using the official PHP SDK (anthropic-ai/sdk, ^0.54 at the time). This records the request shape, the SDK traps, and the design rules that make AI-drafted paperwork safe. Note: the calls were verified against the SDK source and client construction only; no API key existed when this was built, so treat the exact parameters as "compiles and matches the SDK", and re-check against current docs.

Setup traps

Request shape

One helper, claude_json($system, $content, $schema), wraps every call: - $client->beta->messages->create(...), non-streaming, maxTokens: 16000. - outputConfig: ['effort' => 'medium', 'format' => ['type' => 'json_schema', 'schema' => ...]] for guaranteed-parseable JSON instead of forcing a tool call. - Server-side model fallback (fallbacks: 'default' with the matching beta header) so a busy model does not fail the request. - Schemas avoid nullable fields: use 0 and '' as "not present" values; it keeps schemas simple and the PHP side free of null checks. - Reference data that rarely changes (the pricebook: ids, names, aliases) goes in a cached system block (cacheControl ephemeral), so repeated calls do not re-pay for it. - Model rules that shaped this (Opus 5.5 era): forced tool_choice was not supported, thinking could not be disabled, and the default effort is medium, so effort is set explicitly. - Wrap failures in an exception type (AiError) whose message is safe to show the owner; never surface raw API errors to an end user.

Design rules for AI-drafted business documents

See also

Linked from

Sources: plumb memory (plumb-architecture, plumb-gotchas, plumb-canon), 2026-10-05/06