LESSON 20 / 215·2 min readFoundation

Async/await, type hints, and HTTP clients

Must knowConcept lesson
01 Understand02 Recall03 Prepare

Words made simple

A few useful meanings before you read the explanation.

HTTP
The request-and-response protocol used on the web. HTTPS adds encryption to protect information in transit.
Async/await
A programming approach that lets code wait for work such as a network request without blocking other tasks unnecessarily.
AI
Software techniques for tasks such as recognizing patterns, understanding language or generating content.
More words from this lesson
API
A way for one piece of software to ask another for data or an action. A claims API might return the current status of a claim.
Inference
Using a model that has already been trained to produce an answer or prediction.
JSON
A common text format for exchanging data using named fields, lists, numbers and values. Example: {"status": "pending"}.
Schema
A description of the fields and value types that data should contain. It helps check that an answer has the expected shape.
Authentication
Checking who a user or service is, such as by verifying a login session.

Overview

  • Python async def creates a coroutine function; await lets other work proceed while an awaitable operation waits.
  • Type hints describe expected inputs and outputs for developers and type checkers.
  • HTTP clients such as HTTPX provide asynchronous requests, connection pooling, and timeout configuration.

Why this matters

AI services spend much of their time waiting for providers, storage, and internal APIs.

An everyday example

A receptionist answers another call while waiting for a department to reply.

A technical example

An async function awaits a claim-service request instead of blocking the whole request loop.

How it works

  1. An event loop schedules coroutines
  2. Network operations yield while waiting
  3. Completed results resume the coroutine
  4. Bounded concurrency limits load.

Architecture and flow

FastAPI request → async HTTP client → claim API → validated response.

The insurance assistant example

Concurrently retrieve two independent read-only policy sources, but fetch authorization context before using either result.

When to use it

I/O-heavy endpoints and workers with async-compatible libraries.

When to avoid it

  • Async does not make CPU-heavy OCR or local model inference parallel automatically.
  • Do not call synchronous blocking clients inside async routes without deliberate offloading.

Interview answer

“Python async improves I/O concurrency. I use async clients, explicit deadlines, reusable pools, and bounded concurrency; CPU work belongs in workers or suitable processes.”

Benefits, trade-offs, and production considerations

  • Prefer lifecycle-managed clients over creating a new connection pool per request.
  • Propagate cancellation, distinguish connect/read timeouts, and do not create an unbounded gather over a million documents.
  • FastAPI concurrency guide.
Python
import httpx

async def get_claim(client: httpx.AsyncClient, claim_id: str) -> dict:
    # Configured client carries trusted service authentication and base URL.
    response = await client.get(f"/claims/{claim_id}", timeout=5.0)
    response.raise_for_status()
    return response.json()  # Validate the response schema before trusting it.
PAUSE & RECALL

Can you explain it without the notes?

Async/await, type hints, and HTTP clients

Use your own words. Explain an example and one limitation before checking the explanation.

Check your explanation

Python async def creates a coroutine function; await lets other work proceed while an awaitable operation waits. Type hints describe expected inputs and outputs for developers and type checkers. HTTP clients such as HTTPX provide asynchronous requests,…

CONNECT IT TO YOUR INTERVIEW

Turn this concept into a clear answer.

Practice this concept
YOUR NEXT STEP

Keep connecting the ideas.

Next lesson: Pydantic and FastAPI

Adapted from 13_AI.MD · “Async/await, type hints, and HTTP clients”. Examples are study fragments; framework behavior can depend on your version.