Search by

prestashopheaven / runtime-anthropic

georgevanengers

A small client for the Claude Messages API over PHP's curl: request builder with structured outputs and effort, response parser (stop reasons, refusals, cut-off answers, usage), error classes with retry-after, and API keys masked in every message.

Package info

github.com/prestashopheaven/runtime-anthropic

pkg:composer/prestashopheaven/runtime-anthropic

Statistics

Installs: 94

Dependents: 0

Suggesters: 0

Stars: 0

v1.2.1 2026-12-02 16:51 UTC

This package is auto-updated.

Last update: 2026-12-02 17:04:05 UTC


README

A small client for the Claude Messages API over PHP's curl: request builder with structured outputs and effort, response parser (stop reasons, refusals, cut-off answers, usage), error classes with retry-after, and API keys masked in every message.

Use when

A module asks Claude (Anthropic Messages API) something from PHP: transcreation, classification, extraction or a JSON answer that must follow a schema, and needs clear errors (invalid key, no credit, rate limited with retry-after, overloaded, refused, cut off) without an SDK or Composer dependency.

Install

composer require prestashopheaven/runtime-anthropic

Usage

In the module's Infrastructure (an adapter of its own port):

use PrestaShopHeaven\Runtime\Anthropic\ClaudeApiException;
use PrestaShopHeaven\Runtime\Anthropic\ClaudeClient;
use PrestaShopHeaven\Runtime\Anthropic\MessageRequest;

$request = MessageRequest::create('claude-opus-5-5', 32000, $articleText)
    ->withSystem($instructions, true)          // cache breakpoint after the system prompt
    ->withJsonSchema($schema)                  // the answer must follow this schema
    ->withEffort('high')
    ->withRefusalFallback();                   // Opus 5.5 / Sonnet 5.5 only

try {
    $response = (new ClaudeClient())->send($request, $apiKey, 600);
    $fields = $response->json();               // array<string, mixed>
    $tokens = $response->inputTokens + $response->outputTokens;
} catch (ClaudeApiException $e) {
    // $e->getCode(): INVALID_KEY, NO_CREDIT, MODEL_NOT_FOUND, RATE_LIMITED, OVERLOADED, SERVER_ERROR, NETWORK,
    // INVALID_REQUEST, INVALID_RESPONSE, REFUSED, TRUNCATED; $e->isRetryable(), $e->getRetryAfterSeconds().
    // The message never holds the key and is safe to store.
}

(new ClaudeClient())->verify($apiKey, 'claude-sonnet-5-5');   // "Test connection"

API version 2026-10-21 refuses temperature, top_p and top_k, so the builder has none. Nothing is retried by the client: a request costs money, so the caller decides. In a unit test pass your own HttpTransportInterface.

Read-only split of prestashopheaven/mono-runtime-integration: changes, issues and pull requests go there. MIT License.