C.W.K.
Stream
Lesson 02 of 07 · published

Client 초기화 — 1 process 당 1 client

~22 min · client, async, httpx

Level 0Tokenizer
0 XP0/54 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

SDK 는 두 client class 를 줘 — OpenAI() (sync) 와 AsyncOpenAI() (async). 둘 다 내부에서 httpx connection 을 pool 해. App startup 에 한 개 만들어서 끝까지 재사용. Per-call 로 새로 만드는 건 silent performance bug.

왜 1 process 당 1 client?

OpenAI() 호출은 underlying httpx client 를 새로 구성해. httpx client 는 connection pool 을 들고 있어. Per-call construction = 매 request 마다 connection pool 만들고 부숨 = TLS handshake 매번, no keep-alive, no pooling. 부하 들어오면 inexplicable latency tail 과 connection-cap 경고로 만나게 돼.

FastAPI / async framework 안에선 AsyncOpenAI

Sync client 는 event loop 를 block 해. FastAPI, Starlette, aiohttp 안에선 항상 AsyncOpenAI. Async handler 에서 sync OpenAI 섞는 게 'why is my server slow' 버그의 단골.

Per-call override

특정 호출만 다른 timeout 이나 retry 가 필요하면 client.with_options(max_retries=5, timeout=120.0).chat.completions.create(...) — pool 새로 안 만들고 그 호출만 override.

Code

Sync OpenAI() client·python
from openai import OpenAI

# Minimal — reads OPENAI_API_KEY automatically
client = OpenAI()

# Full configuration
client = OpenAI(
    api_key=os.environ["OPENAI_API_KEY"],
    organization="org-XXXXXXXXXXXXXXXX",
    project="proj_XXXXXXXXXXXXXXXX",
    timeout=60.0,       # default is 600s (10 min)
    max_retries=2,      # default is 2
    base_url="https://api.openai.com/v1",  # override for proxies
)
AsyncOpenAI() with custom timeout·python
from openai import AsyncOpenAI

async_client = AsyncOpenAI(
    api_key=os.environ["OPENAI_API_KEY"],
)

# Use in async context
async def main():
    response = await async_client.responses.create(
        model="gpt-5.4",
        input="Hello!",
    )
    print(response.output_text)

External links

Exercise

AsyncOpenAI 쓰는 GET /chat endpoint 1 개짜리 작은 FastAPI 앱 build. Apache Bench 또는 hey 로 100 concurrent request 실행해서 서로 block 안 되는지 검증.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.