Claude CLI 트래픽을 AI Gateway와 Hugging Face로 라우팅하기
Claude CLI 트래픽을 AI Gateway와 Hugging Face로 라우팅하기 (Route Claude CLI traffic through AI Gateway and Hugging Face)
이 튜토리얼에서는 Claude Code CLI의 트래픽을 Kong AI Gateway를 거쳐 Hugging Face 모델로 라우팅하는 방법을 단계별로 소개해요. Hugging Face 토큰을 저장하는 AI Model Provider를 만들고, Claude CLI가 보내는 필드 중 Hugging Face API가 거부하는 값을 걸러내는 AI Policy를 구성해요. 이어서 Anthropic 호환 형식으로 Hugging Face를 가리키는 AI Model을 만들고, Claude CLI의 ANTHROPIC_BASE_URL을 로컬 AI Gateway 엔드포인트로 지정해서 모든 LLM 요청이 게이트웨이를 통과하도록 설정해요
출처: 문서
본문
TL;DR
Hugging Face 모델을 상대로 Claude CLI를 AI Gateway를 통해 어떻게 실행해요?
Hugging Face 토큰을 저장할 AI Provider 엔티티를 만들고, Claude CLI가 보내는 필드 중 Hugging Face API가 거부하는 필드를 제거하는 AI Policy를 만들고, 해당 프로바이더를 통해 Hugging Face로 라우팅되는 Anthropic 호환 형식의 AI Model 엔티티를 만드세요. 그다음 Claude CLI의 ANTHROPIC_BASE_URL을 로컬 AI Gateway 엔드포인트로 지정하면 모든 LLM 요청이 모니터링과 제어를 위해 게이트웨이를 통과해요.
사전 준비
AI Gateway 실행
이 튜토리얼은 Konnect 튜토리얼이며 Konnect 퍼스널 액세스 토큰이 필요해요.
-
Konnect PAT 페이지를 열고 Generate Token을 선택해서 새 퍼스널 액세스 토큰을 만드세요.
-
토큰을 환경 변수로 내보내세요:
export KONNECT_TOKEN='YOUR_KONNECT_PAT' -
AI Gateway 퀵스타트 스크립트를 실행해서 Kong Konnect에 컨트롤 플레인과 데이터 플레인을 자동으로 프로비저닝하고 환경을 구성하세요:
curl -Ls https://get.konghq.com/ai | bash -s -- -k $KONNECT_TOKEN
이렇게 하면 ai-quickstart라는 이름의 AI Gateway 컨트롤 플레인이 설정되고, 로컬 데이터 플레인이 프로비저닝되며, 다음 환경 변수 export가 출력돼요:
export AI_GATEWAY_ID=your-gateway-id
export KONNECT_TOKEN=$KONNECT_TOKEN
export KONNECT_CONTROL_PLANE_NAME=ai-quickstart
export KONNECT_CONTROL_PLANE_URL=https://us.api.konghq.com
export KONNECT_PROXY_URL='http://localhost:8000'
이 내용을 터미널에 복사해서 붙여넣어 세션을 구성하세요.
kongctl v1.20.2+
이 튜토리얼은 kongctl을 사용해 Konnect 리소스를 프로그래밍 방식으로 관리해요. kongctl은 최신 버전(1.20.2)으로 최신 상태를 유지하는 것을 권장해요.
-
developer.konghq.com/kongctl에서 kongctl을 설치하세요.
-
설치를 확인하세요:
kongctl version
Hugging Face
-
추론 권한이 있는 Hugging Face 액세스 토큰을 만드세요.
-
토큰을 bearer 헤더 값으로 내보내세요:
export HUGGINGFACE_AUTH_HEADER='Bearer YOUR_HUGGINGFACE_TOKEN'
Claude Code CLI
-
Claude Code를 설치하세요:
curl -fsSL https://claude.ai/install.sh | bash -
설치를 확인하세요:
claude --version
AI Model Provider, AI Model, AI Policy 만들기
모든 설정이 담긴 AI Model Provider, AI Model, AI Policy를 만들어요
- AI Model Provider 엔티티는 연결을 정의하고 인증 자격 증명을 저장해요.
- AI Request Transformer Policy는 Hugging Face API가 지원하지 않는 불필요한 필드를 제거해요.
- AI Model 엔티티는 사용 가능한 업스트림 모델을 선언하고, 클라이언트 요청의 라우팅 방식을 구성하며, 어떤 AI Model Provider를 사용할지 지정해요.
formats: [type: anthropic]는 업스트림이 Hugging Face임에도 Anthropic 형식의 요청을 받아들여요.
kongctl apply -f - --auto-approve --pat "$KONNECT_TOKEN" << 'EOF'
ai_gateway_model_providers:
- ref: my-huggingface-account
ai_gateway: !lookup {id: !env AI_GATEWAY_ID}
name: my-huggingface-account
display_name: "Hugging Face"
type: huggingface
config:
auth:
type: basic
headers:
- name: Authorization
value: !secret {source: !env HUGGINGFACE_AUTH_HEADER}
ai_gateway_policies:
- ref: strip-claude-beta-info
ai_gateway: !lookup {id: !env AI_GATEWAY_ID}
name: strip-claude-beta-info
display_name: "Strip Claude beta info"
type: request-transformer-advanced
config:
remove:
headers:
- anthropic-beta
querystring:
- beta
body:
- output_config
- context_management
- mcp_servers
- container
- service_tier
- reasoning_effort
ai_gateway_models:
- ref: my-huggingface
ai_gateway: !lookup {id: !env AI_GATEWAY_ID}
name: my-huggingface
display_name: "my-huggingface"
type: model
formats:
- type: anthropic
config:
route:
paths:
- /
model:
body_param: model
values:
- my-huggingface
targets:
- name: deepseek-ai/DeepSeek-V4-Pro-0813
provider: !ref my-huggingface-account#name
config:
type: huggingface
policies:
- !ref strip-claude-beta-info#name
capabilities:
- generate
EOF
{: data-test-step="block" }
AI Model Provider는 다음 설정을 사용해요:
type: huggingface: 이 프로바이더가 Hugging Face의 Messages API 형식으로 통신한다는 것을 지정해요.config.auth.headers[0].value: !secret {source: !env HUGGINGFACE_AUTH_HEADER}: 적용 시점에 환경에서 API 키를 로드해서 설정에 포함되지 않게 하고,kongctl이 plan과 diff 출력에서 이를 가려줘요.
AI Policy는 다음 설정을 사용해요:
-
type: request-transformer-advanced: AI Gateway가 요청을 업스트림으로 전달하기 전에 요청을 수정해요. -
config.remove.headers:anthropic-beta헤더를 제거해요. -
config.remove.querystring:beta쿼리 문자열 파라미터를 제거해요. -
config.remove.body:output_config,context_management,mcp_servers,container,service_tier,reasoning_effort본문 필드를 제거해요.Claude Code의 베타 기능은 버전에 따라 다르고, 시간이 지나면서 다른 호환되지 않는 필드가 추가될 수 있어요. 이 Policy를 적용한 후에도 예상치 못한 필드를 언급하는
400오류가 계속 보이면, 해당 필드를 알맞은remove목록에 추가하고 다시 적용하세요.
AI Model은 다음 설정을 사용해요:
name/display_name: my-huggingface:claude --model에 전달하는 식별자예요. Claude Code는 업스트림 대상 이름이 아니라 이 값을 사용해 모델을 선택해요.formats: [type: anthropic]: 이 모델이 Claude Code가 기본적으로 보내는 형식과 일치하는 Anthropic 호환 형식의 요청을 받아들인다고 선언해요.config.route.paths: [/]: 이 모델의 Routes에 접근할 수 있는 기본 경로를 구성해요.capabilities: [generate]: 텍스트 생성을 활성화해요.anthropic형식을 사용하는 모델의 경우generate는 Anthropic의 기본 Messages API와 일치하는/messages엔드포인트를 만들기 때문에, 기본 경로와 결합하면 클라이언트는/v1/messages로 요청을 보내게 돼요.policies: 이전 단계에서 만든strip-claude-beta-infopolicy를 연결해서, 이 모델을 통해 전송되는 모든 요청에 헤더와 본문 변환이 적용되도록 해요.
Claude Code 실행
Claude Code의 실험용 베타 기능은 AI Policy가 있어도 Hugging Face가 거부하는 필드를 보내요. 이 기능들을 비활성화한 다음, 로컬 AI Gateway 엔드포인트를 가리키는 세션을 시작하세요:
export CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS=1
export ANTHROPIC_BASE_URL=http://localhost:8000/
CLAUDE_CODE_DISABLE_UNKNOWN_MODEL_WINDOW_ENFORCEMENT=1 \
claude --allowedTools "WebSearch,Read" --model "my-huggingface"
그리고 요청이 AI Gateway에 도달하는지 확인하기 위해 질문을 하나 해보세요.
Tell me about the Madrid Skylitzes manuscript.
Claude Code가 질문에 답하기 위해 웹 검색 사용을 승인하라는 메시지를 표시할 수 있어요. Yes를 선택하면 Claude가 요청에 대한 전체 응답을 생성해요:
The Madrid Skylitzes is a remarkable 12th-century illuminated Byzantine
manuscript that represents one of the most important surviving examples
of medieval historical documentation. Here are the key details:
What it is
The Madrid Skylitzes is the only surviving illustrated manuscript of John
Skylitzes' "Synopsis of Histories" (Σύνοψις Ἱστοριῶν), which chronicles
Byzantine history from 811 to 1057 CE - covering the period from the death
of Emperor Nicephorus I to the deposition of Michael VI.
Artistic Significance
- 574 miniature paintings (with about 100 lost over time)
- Lavishly decorated with gold leaf, vibrant pigments, and intricate
detailing
- Depicts everything from imperial coronations and battles to daily life
in Byzantium
- The only surviving Byzantine illuminated chronicle written in Greek
Unique Collaboration
The manuscript is believed to be the work of 7 different artists from
various backgrounds:
- 4 Italian artists
- 1 English or French artist
- 2 Byzantine artists
{:.no-copy-code}
관련 리소스
더 알아보기 (Learn more)
- AI Gateway의 개념과 기능 전반을 다루는 기본 문서 — AI Gateway
- 프로바이더 연결과 인증 자격 증명을 저장하는 엔티티 설명 — AI Model Provider
- 모델 형식과 라우팅 대상을 정의하는 엔티티 참고 — AI Model
- Hugging Face가 거부하는 필드를 요청에서 제거할 때 쓰는 정책 안내 — AI Request Transformer Policy
- Hugging Face 프로바이더 연동 설정과 요구 사항 정리 — Hugging Face provider
- Konnect 리소스를 명령줄에서 관리하는 kongctl 사용법 — kongctl