Skip to content

LLM providers

LLM provider selection and parity. For integrators choosing a provider and setting models.

Select a provider

from prettyplay.config import PrettyConfig
from prettyplay.llm import create_provider

config = PrettyConfig(provider="anthropic", model="claude-sonnet-4-5")
provider = create_provider(config)

The provider is a project setting: openai or anthropic; env override PRETTYPLAY_PROVIDER. API keys come only from environment variables: OPENAI_API_KEY for openai, ANTHROPIC_API_KEY for anthropic — read lazily on the first request.

Models

Setting Purpose Fallback
model the main model for both operations —
generation_model code generation only model
classification_model failure classification, the group diagnosis and the compliance gate model

The compliance gate runs on the effective classification model (classification_model or model) — never on the model that wrote the candidate.

base_url overrides the provider endpoint when set.

Parity

Both providers expose the same four operations — generate_step_code, classify_step_failure, classify_group_failure and check_instruction_compliance — with identical inputs, identical output shapes and the identical failure taxonomy: a provider service failure raises LLMUnavailableError; cached step code never depends on the provider. Each engine attempt makes one logical LLM request; transient transport failures can resend that request without consuming generation or healing attempts.

llm_request_attempts defaults to three total sends, including the initial send. Connection failures, timeouts, HTTP 408/429 and 5xx responses are retried. Authentication, authorization, invalid requests and explicitly exhausted quota fail immediately. Pauses use a fixed 1, 2, 4, 8, 10… second base plus up to 25% jitter, capped at 10 seconds. Retry-After up to 10 seconds never lengthens the loop pause; a longer value ends the request with an actionable LLMUnavailableError. Exhaustion also raises that error with the original SDK failure chained. Set the attempt count to 1 to disable transport retries. This policy applies to custom base_url endpoints too. An empty completion body has its own bounded channel: at most one re-send within the same total budget; another empty body re-raises the closure's LLMUnavailableError unchanged. Other malformed answer content is parsed after the send, outside transport retries.

User instructions parity: each operation carries its own instructions — generation requests render the generation_prompt setting, classification requests render the classification_prompt setting — as a verbatim USER INSTRUCTIONS block with identical placement semantics in both providers. A parity requirement, not a capability difference: classification requests never carry the generation instructions and generation requests never carry the classification instructions.

Regeneration block parity: every generation request renders a STEP TYPE line (action or assertion) immediately before the STEP line and may carry three extra blocks after the PLAYWRIGHT SKILL and the optional USER INSTRUCTIONS blocks — HISTORY (the verbatim per-step attempt records, when present: each record carries the attempt outcome, the URL before -> after line, the complete candidate code and the complete error, with the original cached code anchored as record 0 on a healing path),RECOMMENDATION (the diagnosis of the classification that preceded the regeneration, when present — on a retry after a gate block it also carries the standing high finding as an imperative prohibition, composed with the entry diagnosis on a healing path) and USER GUIDANCE (the engineer guidance message of the interactive steering, when present) — rendered in this fixed order, identically in both providers. An unset input renders no block. Unrecognized classification labels fall back to incurable in both providers alike.

Group framing parity: a generation, regeneration or guided request of a step inside a group carries the group framing — a GROUP PROMPT block with the group prompt verbatim, rendered immediately before the PREVIOUS STEPS block, whose group entries are marked (- {sentence} [group step — {group prompt}]; ordinary entries render as the plain sentence). None renders no block and no marking, identically in both providers. The framing is an input of the request, never a capability difference.

Structure-registry parity: a generation, regeneration or guided request may carry the rendered structural element registry of the current page — one line per interactive element with its runtime-verified unique selector (the structure_registry capture of the page handle — see Driver facade); a non-empty registry renders as its own PAGE STRUCTURE block immediately after the PAGE SNAPSHOT block, identically in both providers; empty — no block, the request identical to the snapshot-only one. Supplied by every engine generation and healing request and by the guided requests of the interactive steering when the send_structure setting is on (the default) — see Configuration. A parity requirement, not a capability difference.

Page-URL parity: a generation request may carry the current page URL — a non-empty page_url renders as its own PAGE URL line immediately after the PAGE STRUCTURE block, or immediately after the PAGE SNAPSHOT block when no registry was sent, identically in both providers; None renders no line. Supplied by every engine generation and healing request and by the guided requests of the interactive steering. A parity requirement, not a capability difference.

Skill parity: every generation request renders the PLAYWRIGHT SKILL block after the scenario inputs and immediately before the USER INSTRUCTIONS block — the packaged Playwright skill (prettyplay/skills/playwright/SKILL.md, read once per runtime) supplied by the calling engine; guidance, not an allowlist. Both providers render it identically at the same position. A parity requirement, not a capability difference.

The one transport-level asymmetry: the anthropic Messages API requires max_tokens, so anthropic requests carry a completion cap — the request_max_tokens setting (default 16384, sized so a reasoning model never truncates: thinking blocks spend the same budget before the text block, and a cap-truncated response carries no text block at all, an empty completion); the openai side sends no cap and the model maximum applies.

Answer shape

generate_step_code returns step code of the fixed form (see Driver facade). Models often answer with a fenced python block (```python … ```); the provider unwraps the first fenced block before returning, so the engine receives clean code either way — an answer with no closed fence is returned verbatim and, if unparsable, keeps failing downstream in execution.

Classification

classification = provider.classify_step_failure(
    prompt=system_prompt,  # the system prompt text comes from the calling engine
    user_instructions="",  # the classification instructions from the classification_prompt setting; empty — no block
    step_text="click the «Sign in» button",
    code=step_code,
    error="element not found: button «Sign in»",
    snapshot=snapshot_text,
    screenshot=None,
)
print(classification.category, classification.explanation, classification.recommendation)

The classification categories — rot, product_defect, fixable, incurable — and their consequences are covered in Self-healing.

A non-empty user_instructions renders as a separate USER INSTRUCTIONS block in the request — the final block of the user content, after all classification inputs.

The group diagnosis operation

classify_group_failure is the diagnosis request of the group recovery (see Groups): one request per recovery cycle, carrying the group prompt verbatim, the composed traces of the group's steps, the failed step's sentence and attempt history, the page snapshot and the optional screenshot — sent through the effective classification model (classification_model or model) with the project's classification_prompt instructions, full parity between the providers. The answer parses strictly; a degraded answer maps to the conservative incurable with the raw answer logged — never a granted regeneration.

The compliance operation

check_instruction_compliance is the verdict request of the compliance gate (see Configuration): the engine calls it before caching a successfully executed candidate, with one bounded re-ask after a malformed verdict — judging both the instruction compliance and the step adequacy in one request — never for replayed cached code, never when generation_approve is off or generation_prompt is empty (zero calls).

findings = provider.check_instruction_compliance(
    prompt=system_prompt,  # the gate system prompt text comes from the calling engine
    user_instructions=instructions,  # the project's user instructions (the generation_prompt setting)
    instruction="click the «Sign in» button",  # the prepared plain-text instruction, never the raw template sentence
    step_type="action",  # action | assertion — the adequacy dimension judges by it
    inputs={"code": "A1"},  # the call-local input bindings of the step; empty — no INPUTS block
    declarations=["title"],  # the reached capture names of the step; empty — no RESULTS block
    code=step_code,
    attempt_history=[r.render() for r in history],  # the verbatim per-step attempt records, when present
    structure=registry_text,  # the element registry the generation request carried; empty — no PAGE STRUCTURE block
)
  • full parity between the providers: the user content carries the blocks in the fixed order — INSTRUCTIONS (when configured), STEP (with its STEP TYPE line and the optional INPUTS and RESULTS sections), ATTEMPT HISTORY (omitted when the step has no attempt records), the optional PAGE STRUCTURE (when the generation request carried the element registry — the runtime-verified selectors the gate checks the candidate's locators against) and CODE — built identically by both through one shared builder; no screenshot input on this operation
  • the gate model is the effective classification model (classification_model or model)
  • the answer parses strictly: a JSON list of findings, each with instruction, priority (high|medium|low), explanation and dimension (instruction|adequacy) — the instruction dimension quotes the violated instruction, the adequacy dimension names the fragment of the step sentence the code fails to accomplish, judged from the step type and the attempt history; an empty list [] means compliant; a JSON syntax glitch of the answer is salvaged once (the json-repair library) before the validation — a model dropping a quote, a comma or a bracket does not fail the run — and a malformed verdict (an answer of the old shape — a finding without a dimension — included) raises ComplianceVerdictError — never a silent pass. Only a high finding — in either dimension — blocks the candidate, and that decision belongs to the calling engine, not the provider
  • SDK transport errors follow the same bounded retry policy as the other operations; a permanent rejection or exhausted request raises LLMUnavailableError
  • one logical verdict request per candidate; resends do not rerun the candidate's browser actions