Skip to content

prompt_receipt

prompt_receipt

Per-attempt prompt receipt: what reached the model, by source, as hashes and sizes.

A PromptReceipt answers "which injected files, how many bytes, which hash" for one assembled prompt without keeping any prompt text. It is built where the prompt is assembled (PromptRenderer.render), carried to the conductor on SheetAttemptResult.prompt_receipt and stored on SheetState.prompt_receipt (GH #546).

Token counts are estimates from marianne.core.tokens.estimate_tokens (a character heuristic), never a tokenizer result.

Classes

PromptReceiptSource

Bases: BaseModel

One source that contributed (or was refused) bytes to a sheet's prompt.

PromptReceipt

Bases: BaseModel

What one attempt's assembled prompt was made of. Never holds prompt text.

Functions:

sha256_label

sha256_label(data)

sha256:<hex> for data (the form the context-delivery receipts use).

Source code in src/marianne/core/prompt_receipt.py
def sha256_label(data: bytes) -> str:
    """``sha256:<hex>`` for ``data`` (the form the context-delivery receipts use)."""
    return f"sha256:{hashlib.sha256(data).hexdigest()}"

build_receipt

build_receipt(*, prompt, preamble, sources)

Build a receipt from the assembled prompt and the sources recorded for it.

Source code in src/marianne/core/prompt_receipt.py
def build_receipt(
    *,
    prompt: str,
    preamble: str,
    sources: Sequence[PromptReceiptSource],
) -> PromptReceipt:
    """Build a receipt from the assembled prompt and the sources recorded for it."""
    prompt_bytes = prompt.encode("utf-8")
    delivered = sum(s.bytes for s in sources if s.kind != "template" and not s.dropped_reason)
    listed = list(sources[:MAX_RECEIPT_SOURCES])
    overflow = list(sources[MAX_RECEIPT_SOURCES:])
    return PromptReceipt(
        sources=listed,
        sources_omitted=len(overflow),
        sources_omitted_bytes=sum(s.bytes for s in overflow),
        template_bytes=max(0, len(prompt_bytes) - delivered),
        preamble_bytes=len(preamble.encode("utf-8")),
        total_bytes=len(prompt_bytes),
        estimated_tokens=estimate_tokens(prompt) + estimate_tokens(preamble),
        prompt_sha256=sha256_label(prompt_bytes),
    )