#!/usr/bin/env python3
"""Run: python3 verify-long-context.py. Synthetic sizing/billing, zero model calls."""

from decimal import Decimal
from fractions import Fraction
import json


def kv_bytes(tokens, layers=32, kv_heads=8, head_dim=128, element_bytes=2):
    return 2 * layers * tokens * kv_heads * head_dim * element_bytes


def causal_pairs(tokens):
    return tokens * (tokens + 1) // 2


def main():
    lengths = (256_000, 1_000_000, 10_000_000)
    rows = [
        {
            'tokens': length,
            'hypothetical_kv_bytes': kv_bytes(length),
            'hypothetical_kv_decimal_GB': str(Decimal(kv_bytes(length)) / 10**9),
            'hypothetical_kv_GiB': str(Decimal(kv_bytes(length)) / 1024**3),
            'causal_query_key_pairs_per_head': causal_pairs(length),
        }
        for length in lengths
    ]
    assert kv_bytes(8192) == 1024**3  # Same hypothetical layout as the preceding post.
    assert kv_bytes(256_000) == 33_554_432_000
    assert kv_bytes(1_000_000) == 131_072_000_000
    assert kv_bytes(10_000_000) == 1_310_720_000_000
    assert kv_bytes(10_000_000) == 10 * kv_bytes(1_000_000)
    pairs_ratio = Fraction(causal_pairs(10_000_000), causal_pairs(1_000_000))
    assert Fraction(999, 10) < pairs_ratio < 100

    # Public Codex regression-test example, not this user's live catalog/settings.
    configured = 272_000
    usable = configured * 95 // 100
    compact = configured * 90 // 100
    assert (usable, compact) == (258_400, 244_800)

    # A valid *hypothetical* million-token total request at dated Sol API rates.
    # Ordinary uncached input; excludes tools, regional/mode modifiers and writes.
    input_tokens, billed_output_tokens = 900_000, 100_000
    assert input_tokens <= 922_000
    assert billed_output_tokens <= 128_000
    assert input_tokens + billed_output_tokens <= 1_050_000
    assert input_tokens + billed_output_tokens == 1_000_000
    assert input_tokens > 272_000  # Long-context rates apply to the full request.
    input_usd = Decimal(input_tokens) * Decimal(4) / 1_000_000
    output_usd = Decimal(billed_output_tokens) * Decimal(15) / 1_000_000
    eligible_cached_input_usd = Decimal(input_tokens) * Decimal('0.20') / 1_000_000
    assert input_usd + output_usd == Decimal('5.10')
    assert eligible_cached_input_usd + output_usd == Decimal('1.68')
    print(json.dumps({
        'kind': 'hypothetical arithmetic, not allocation, inference or accuracy testing',
        'model_calls': 0,
        'checked_on': '2026-11-11',
        'sizing': rows,
        'tenfold_length_full_causal_attention_pair_ratio': str(pairs_ratio),
        'codex_source_test_example': {
            'configured': configured, 'usable_at_95_percent': usable,
            'normal_total_scope_compaction_at_90_percent': compact,
        },
        'hypothetical_sol_standard_long_context_bill': {
            'input_tokens': input_tokens, 'billed_output_tokens': billed_output_tokens,
            'ordinary_uncached_input_usd': str(input_usd),
            'output_usd': str(output_usd),
            'total_usd': str(input_usd + output_usd),
            'if_all_input_eligible_cache_reads_total_usd': str(eligible_cached_input_usd + output_usd),
        },
    }, indent=2))


if __name__ == '__main__':
    main()
