Recipes · SDK Patterns · Updated Nov 2026

API Integration Recipes

1. Deterministic JSON Extraction

Keep temperature low and enforce a schema at the boundary so parsers never break on conversational prose.

import json

def parse_strict(raw_text: str, required_keys: set[str]) -> dict:
    data = json.loads(raw_text)
    missing = required_keys - data.keys()
    if missing:
        raise ValueError(f"Missing required keys: {sorted(missing)}")
    return data

2. Retry with Exponential Backoff & Jitter

import random, time

def with_backoff(fn, max_attempts=4):
    for attempt in range(max_attempts):
        try:
            return fn()
        except TimeoutError:
            if attempt == max_attempts - 1:
                raise
            time.sleep((2 ** attempt) + random.uniform(0, 0.5))

3. Measuring Token & Latency Budgets

Log input tokens, cached tokens, output tokens, and time-to-first-token per workflow step. Most cost spikes come from unbounded retry loops rather than base prompt length.