Recipe
Python trio primer
Meridian's HTTP surface is plain OpenAI-compatible JSON, which means any async Python runtime works. This recipe walks through using trio with structured concurrency to fan out parallel calls against the gateway without tripping rate limits or leaking tasks on cancellation.
1. Install the dependencies
Trio plays well with httpx because httpx ships a native trio transport. Pin both so cancellation semantics stay consistent across environments.
pip install "trio>=0.24" "httpx[trio]>=0.27"
2. Open a nursery for parallel calls
Nurseries give you a structured scope. Every task spawned with start_soon is awaited before the block exits, and any exception cancels its siblings. Meridian's gateway tolerates bursts up to your tier ceiling, so a nursery is the right unit.
import trio
import httpx
async def fetch_status(client: httpx.AsyncClient, url: str) -> None:
response = await client.get(url, headers={"x-meridian-key": "sk-..."})
print(url, response.status_code)
async def main() -> None:
urls = [
"https://llm.getnimbus.net/v1/models",
"https://llm.getnimbus.net/v1/health",
"https://llm.getnimbus.net/v1/usage",
]
async with httpx.AsyncClient(timeout=15.0) as client:
async with trio.open_nursery() as nursery:
for url in urls:
nursery.start_soon(fetch_status, client, url)
if __name__ == "__main__":
trio.run(main)3. Cancel cleanly on the first failure
Wrap the nursery in move_on_after or fail_after to bound total latency. Because trio uses checkpoints rather than thread interrupts, an in-flight httpx request will close its socket cleanly when the scope cancels — no half-written billing records on the Meridian side.
- Use one AsyncClient per nursery so the connection pool is shared.
- Surface partial results through a trio.MemoryChannel if a caller needs streaming.
- Set timeout=httpx.Timeout(15.0, connect=5.0) to keep slow upstreams from starving the nursery.