AI Agent Development and Interview Guide: 01-Python Async Backend and Engineering Fundamentals

Covers production-grade Agent backend fundamentals including asyncio concurrency, Semaphore rate limiting, timeout controls, exponential backoff retries with jitter, and idempotency.

Contents20 sections

Chapter 1: Python Async Backend and Engineering Fundamentals

In job recruitment descriptions, 'Proficient in Python' does not mean writing simple data processing scripts—it means building maintainable, testable, observable, and concurrent production services.Agent systems concurrently access model APIs, vector databases, relational databases, and external tools. Because most waiting occurs during network I/O, async execution, timeouts, concurrency control, and error handling form essential foundation skills.

1. Synchronous, Concurrency, and Parallelism

technical system component:

  • Synchronous execution:technical system component。
  • concurrency:technical system component,technical system component I/O technical system component。
  • parallelism:technical system component CPU technical system component,technical system component CPU technical system component。

LLM API calls are inherently I/O-bound operations.Embedding technical system component I/O;technical system component,technical system component CPU/GPU technical system component。asyncio technical system component,technical system component。

FastAPI official guidelines recommend using async def and await when third-party libraries provide awaitable interfaces; blocking libraries should be routed into standard def functions or thread pools to avoid blocking the event loop.technical system component FastAPI technical system component。

🔥 P0 High-Frequency Must-Know: async technical system component?
technical system component。technical system component I/O technical system componentconcurrencytechnical system component。CPU technical system component、technical system componentparallelismtechnical system component、GPU technical system component。technical system component async def,technical system component。

2. A Concurrency-Controlled Model Gateway

The following code demonstrates four critical production engineering points: concurrency caps, timeouts, exponential backoff retries, and structured errors.

PYTHON
import asyncio
import random
from dataclasses import dataclass
from typing import Protocol


class ModelClient(Protocol):
    async def generate(self, prompt: str) -> str: ...


@dataclass
class ModelCallError(Exception):
    code: str
    message: str
    retryable: bool


class ReliableModelGateway:
    def __init__(
        self,
        client: ModelClient,
        max_concurrency: int = 8,
        timeout_seconds: float = 20.0,
        max_attempts: int = 3,
    ) -> None:
        self.client = client
        self.semaphore = asyncio.Semaphore(max_concurrency)
        self.timeout_seconds = timeout_seconds
        self.max_attempts = max_attempts

    async def generate(self, prompt: str) -> str:
        async with self.semaphore:
            for attempt in range(1, self.max_attempts + 1):
                try:
                    async with asyncio.timeout(self.timeout_seconds):
                        return await self.client.generate(prompt)
                except TimeoutError:
                    error = ModelCallError(
                        code="MODEL_TIMEOUT",
                        message=f"model timed out after {self.timeout_seconds}s",
                        retryable=True,
                    )
                except ConnectionError as exc:
                    error = ModelCallError(
                        code="MODEL_NETWORK_ERROR",
                        message=str(exc),
                        retryable=True,
                    )
                except ValueError as exc:
                    raise ModelCallError(
                        code="MODEL_BAD_REQUEST",
                        message=str(exc),
                        retryable=False,
                    ) from exc

                if attempt == self.max_attempts or not error.retryable:
                    raise error

                base = 0.25 * (2 ** (attempt - 1))
                await asyncio.sleep(base + random.uniform(0, 0.1))

        raise RuntimeError("unreachable")

Key engineering details often overlooked in production:

  1. Semaphore technical system component,technical system component。
  2. technical system component,technical system component。
  3. technical system component。technical system component、technical system component、technical system component。
  4. technical system component,technical system component、technical system component。

🔥 P0 High-Frequency Must-Know: technical system componentexponential backofftechnical system componentrandom jitter?
technical system component。exponential backofftechnical system component,random jittertechnical system component。technical system component、technical system component。

3. FastAPI Service Boundaries

The API layer should not be bloated with Agent logic directly. A 4-layer architecture is recommended:

TEXT
HTTP/API technical system component -> technical system component -> technical system component/technical system component -> technical system component
  • API technical system component:technical system component、technical system component、technical system component ID、HTTP technical system component。
  • technical system component:technical system component Agent Run、technical system component、technical system component。
  • technical system component:technical system component、technical system component、technical system component。
  • technical system component:models、technical system component、technical system component、technical system component。

technical system component:

PYTHON
from typing import Literal
from uuid import UUID, uuid4

from fastapi import FastAPI, Header, HTTPException
from pydantic import BaseModel, Field

app = FastAPI()


class RunRequest(BaseModel):
    query: str = Field(min_length=1, max_length=4000)
    mode: Literal["read_only", "allow_write"] = "read_only"


class RunAccepted(BaseModel):
    run_id: UUID
    status: Literal["accepted"] = "accepted"


@app.post("/v1/runs", response_model=RunAccepted, status_code=202)
async def create_run(
    body: RunRequest,
    idempotency_key: str = Header(min_length=8),
) -> RunAccepted:
    if body.mode == "allow_write" and not idempotency_key:
        raise HTTPException(status_code=400, detail="missing idempotency key")

    run_id = uuid4()
#    # technical system component,technical system component HTTP technical system component。
    return RunAccepted(run_id=run_id)

technical system component 202

Long-running Agent runs may take tens of seconds or even minutes. Synchronous HTTP waiting consumes connections and complicates retry semantics.technical system component:

  1. POST /runs technical system component 202 Accepted technical system component run_id
  2. technical system component Worker technical system component;
  3. technical system component、SSE technical system component WebSocket technical system component;
  4. technical system component、technical system component。

technical system component、technical system component、technical system component;technical system component。

4. Idempotency: Essential Backend Engineering for Tool Calling

If a network request times out, the caller cannot determine whether side-effects succeeded downstream. Retrying blindly risks duplicate side-effects. The solution is enforcing idempotency.

PYTHON
from dataclasses import dataclass


@dataclass
class CreateTicketCommand:
    idempotency_key: str
    user_id: str
    title: str
    description: str


async def create_ticket(command: CreateTicketCommand, repository) -> str:
    existing = await repository.find_by_idempotency_key(command.idempotency_key)
    if existing:
        return existing.ticket_id

#    # technical system component idempotency_key technical system component,technical system componentconcurrencytechnical system component。
    ticket = await repository.insert(command)
    return ticket.ticket_id

technical system component,technical system component“technical system component”technical system componentconcurrencytechnical system component。

🔥 P0 High-Frequency Must-Know: technical system component?
Retries recover from transient network failures, while idempotency ensures that repeated retries do not trigger duplicate side-effects.technical system component;technical system component、technical system component、state machinetechnical system component。

5. Do Not Block the Event Loop

technical system component:

PYTHON
import time

async def bad_handler():
    time.sleep(5)  # technical system component

technical system component,technical system component:

PYTHON
import asyncio

async def parse_pdf(path: str) -> str:
    return await asyncio.to_thread(blocking_pdf_parser, path)

technical system component。technical system component、OCR technical system component Worker/technical system component,technical system componentconcurrencytechnical system component。

6. Error Classification and Recovery Strategies

technical system componenttechnical system componenttechnical system component
technical system component429、technical system component、technical system component 5xxtechnical system component、technical system component、technical system component
technical system componentJSON technical system component、technical system componenttechnical system component;technical system component
technical system componenttechnical system component、technical system componenttechnical system component
technical system componenttechnical system componenttechnical system component、technical system component,technical system component
technical system componenttechnical system component、technical system componenttechnical system component,technical system component Agent technical system component
technical system componenttechnical system component、technical system componenttechnical system component Trace,technical system component

technical system component。technical system component,technical system component、technical system component、technical system component。

7. Testing an Agent Tool

Do not call live external systems during unit testing. Use dependency injection to pass Fake adapters:

PYTHON
import pytest


class FakeRepository:
    def __init__(self):
        self.items = {}

    async def find_by_idempotency_key(self, key):
        return self.items.get(key)

    async def insert(self, command):
        ticket = type("Ticket", (), {"ticket_id": "T-001"})()
        self.items[command.idempotency_key] = ticket
        return ticket


@pytest.mark.asyncio
async def test_create_ticket_is_idempotent():
    repo = FakeRepository()
    command = CreateTicketCommand("req-12345", "u-1", "technical system component", "technical system component")

    first = await create_ticket(command, repo)
    second = await create_ticket(command, repo)

    assert first == second == "T-001"
    assert len(repo.items) == 1

Demonstrating automated unit tests verifying idempotent write execution is far more convincing in interviews than static success screenshots.

technical system component A:concurrencytechnical system component

technical system component:

  • technical system componentconcurrency 5 technical system component;
  • technical system component 10 technical system component;
  • 429 technical system component 3 technical system component;
  • technical system component,technical system component。

technical system component:technical system component Fake Client technical system component、429 technical system component,technical system component 5 technical system component。

technical system component B:technical system component API

technical system component /runs/runs/{id}/runs/{id}/cancel technical system component。technical system component:queuedrunningwaiting_approvalsucceededfailedcancelled

technical system component:technical system component,technical system component succeeded -> running

technical system component C:technical system component

technical system component“technical system component”technical system component:

  • Pydantic technical system component;
  • technical system component;
  • technical system component;
  • technical system component;
  • technical system component;
  • technical system component。

9. High-Frequency Interview Q&A

🔥 P0:async def technical system component?

technical system component,technical system component Worker technical system component。technical system component、technical system component,technical system component CPU/technical system component。technical system componentconcurrencytechnical system component。

🔥 P0:technical system component API technical system component?

technical system component;technical system component;technical system componentexponential backofftechnical system component;technical system component;technical system componentidempotency;technical system component。

🔥 P0:HTTP technical system component Agent technical system component?

technical system component,technical system component,technical system component。technical system component 202 + run_id,technical system component,technical system component SSE/WebSocket/technical system component。

⭐ P1:concurrencytechnical system component?

technical system component:technical system component;models/technical system component Semaphore technical system component;technical system component Worker concurrencytechnical system component。technical system component。

⭐ P1:technical system component?

technical system component、technical system component、technical system componentstate machine。technical system component,technical system component,technical system component,technical system component Prompt technical system component。

10. Chapter Completion Standards

  • technical system component I/O concurrencytechnical system component CPU parallelismtechnical system component;
  • technical system component、technical system component、technical system componentconcurrencytechnical system component;
  • technical system componentstate machine;
  • technical system component;
  • technical system component、technical system component、technical system component GPU technical system component。

REFERENCES

References

  1. 01FastAPI 异步文档

Series

AI Agent Development and Interview Guide

Next step

Continue with related topics

Continue along the same topic.

Browse latest news