Skip to content

OpenAI 호출을 Cloudflare AI Gateway 경유로 전환 - #51

Merged
DaleSeo merged 2 commits into
mainfrom
feat/openai-via-ai-gateway
Aug 8, 2026
Merged

OpenAI 호출을 Cloudflare AI Gateway 경유로 전환#51
DaleSeo merged 2 commits into
mainfrom
feat/openai-via-ai-gateway

Conversation

@DaleSeo

@DaleSeo DaleSeo commented Aug 8, 2026

Copy link
Copy Markdown
Member

OpenAI API를 직접 호출 방식에서 Cloudflare AI Gateway를 통한 경유 호출로 변경하고자 합니다.

Cloudflare AI Gateway를 사용하면 여러 AI 제공업체의 API 키를 한 곳에서 관리할 수 있기 때문에 노출 시 회전도 용이하고, 나중에 트래픽이 많아졌을 때 Throttling, 한도 설정, 캐싱 설정도 가능합니다.

변경 사항은 많지 않습니다. 호출 URL 과 인증 헤더만 바뀌고 요청 본문은 그대로입니다.

- fetch("https://api.openai.com/v1/chat/completions", {
-   headers: { Authorization: `Bearer ${apiKey}` , ... }
+ fetch(OPENAI_CHAT_COMPLETIONS_URL, {
+   headers: { "cf-aig-authorization": `Bearer ${gatewayToken}`, ...AI_GATEWAY_RETRY_HEADERS }

4곳에 중복돼 있던 URL 은 utils/constants.js 로 모았습니다.

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Aug 8, 2026

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
github 9b9d10c Commit Preview URL

Branch Preview URL
Aug 08 2026, 04:50 PM

@DaleSeo DaleSeo closed this Aug 8, 2026
@DaleSeo DaleSeo reopened this Aug 8, 2026
@DaleSeo
DaleSeo force-pushed the feat/openai-via-ai-gateway branch from 9b5f17f to 9d54eb6 Compare August 8, 2026 01:36
@DaleSeo
DaleSeo marked this pull request as draft August 8, 2026 01:37
PR 이벤트마다 도는 워크로드라 게이트웨이의 로그·캐싱·속도 제한·지출 한도가
전부 실효가 있다. 재시도도 게이트웨이에 맡겨 Worker CPU 예산을 쓰지 않는다.

OpenAI 키는 게이트웨이에 저장돼 있어 Worker 에서 제거하고,
env.OPENAI_API_KEY 를 env.AI_GATEWAY_TOKEN 으로 대체했다.
@DaleSeo
DaleSeo force-pushed the feat/openai-via-ai-gateway branch from 9d54eb6 to 6b709d0 Compare August 8, 2026 01:39
@DaleSeo
DaleSeo marked this pull request as ready for review August 8, 2026 01:52

@parkhojeong parkhojeong left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 Cloudflare 경유하면 여러 이점이 있군요! 좋은 거 같네요. 리뷰 슬쩍 남겨봅니다~

Comment thread handlers/complexity-analysis.js Outdated
method: "POST",
headers: {
Authorization: `Bearer ${apiKey}`,
"cf-aig-authorization": `Bearer ${gatewayToken}`,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 헤더가 네 군데에서 사용되는데 이것도 한 곳에서 관리하도록 하는건 어떨까요?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

좋은 제안이네요. 아예 헤더 조립을 함수로 감쌌습니다.

Comment thread handlers/complexity-analysis.js Outdated
Comment on lines +13 to +14
import { AI_GATEWAY_RETRY_HEADERS, OPENAI_CHAT_COMPLETIONS_URL } from "../utils/constants.js";

@parkhojeong parkhojeong Aug 8, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI_GATEWAY_RETRY_HEADERS가 import 되었는데 사용되는 곳이 없네요. 헤더에 사용하는 부분이 누락된 거 같습니다.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

@DaleSeo
DaleSeo requested a review from parkhojeong August 8, 2026 16:53
@DaleSeo

DaleSeo commented Aug 8, 2026

Copy link
Copy Markdown
Member Author

@parkhojeong 리뷰 감사합니다. 피드백 반영하였습니다. 재검토 후에 추가 의견없으시면 승인 부탁드리겠습니다. write 권한드렸으니 가능하실 거에요.

@parkhojeong parkhojeong left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

함수로 감싸는거 좋네요. 수고하셨습니다!

@DaleSeo
DaleSeo merged commit ddb38ea into main Aug 8, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants