pyai.docs

AI Engineering › 12 of 14

Calling LLM APIs

HTTP requests, async, and structured outputs.

python
import asyncio, httpx, os

async def ask(client, prompt):
    r = await client.post(
        "https://api.example.com/v1/chat/completions",
        headers={"Authorization": f"Bearer {os.environ['API_KEY']}"},
        json={"model": "gpt-x", "messages": [{"role": "user", "content": prompt}]},
        timeout=30,
    )
    r.raise_for_status()
    return r.json()["choices"][0]["message"]["content"]

async def main():
    async with httpx.AsyncClient() as c:
        answers = await asyncio.gather(*(ask(c, p) for p in prompts))
Tip: Keep keys in environment variables; never hardcode them.

⚡ Quick check

  • ✓ asyncio.gather for concurrency
  • ✓ raise_for_status()
  • ✓ Secrets via os.environ