Google AI Studio SDK
Google AI Studio SDK
Google AI Studio(Generative Language API)의 패스스루 엔드포인트를 소개할게요. Google 고유 엔드포인트를 네이티브 형식 그대로(변환 없이) 호출할 수 있는 기능이에요. /generateContent 같은 엔드포인트를 그대로 사용할 수 있어요.
출처: 문서
본문
연결 대상 호스트는 https://generativelanguage.googleapis.com이고, 프록시를 통한 주소는 이렇게 구성돼요.
LITELLM_PROXY_BASE_URL/gemini
사용 예시 (Example Usage)
토큰 계산(countTokens) 엔드포인트를 호출하는 예시예요. 쿼리 파라미터 key에 LiteLLM 키를 넣으면 돼요.
curl 'http://0.0.0.0:4000/gemini/v1beta/models/gemini-3.8-flash:countTokens?key=sk-anything' \
-H 'Content-Type: application/json' \
-d '{
"contents": [{
"parts":[{
"text": "The quick brown fox jumps over the lazy dog."
}]
}]
}'
Google AI Studio JS SDK(@google/genai)를 프록시로 연결하는 예시예요. httpOptions.baseUrl을 /gemini 경로로 지정하면 돼요.
const { GoogleGenAI } = require("@google/genai");
const ai = new GoogleGenAI({
apiKey: "sk-<y...y>", // litellm proxy API key
httpOptions: {
baseUrl: "http://localhost:4000/gemini", // http://<proxy-base-url>/gemini
},
});
async function main() {
try {
const response = await ai.models.generateContent({
model: "gemini-3.8-flash",
contents: "Explain how AI works",
});
console.log(response.text);
} catch (error) {
console.error('Error:', error);
}
}
// For streaming responses
async function main_streaming() {
try {
const response = await ai.models.generateContentStream({
model: "gemini-3.8-flash",
contents: "Explain how AI works",
});
for await (const chunk of response) {
process.stdout.write(chunk.text);
}
} catch (error) {
console.error('Error:', error);
}
}
main();
// main_streaming();
빠른 시작 (Quick Start)
/countTokens 엔드포인트를 호출하는 예시예요. 먼저 API 키를 환경 변수로 설정해요.
export GEMINI_API_KEY=""
그다음 LiteLLM 프록시를 실행해요.
litellm
# RUNNING on http://0.0.0.0:4000
이제 countTokens 엔드포인트를 호출해요.
curl http://0.0.0.0:4000/gemini/v1beta/models/gemini-3.8-flash:countTokens?key=anything \
-H 'Content-Type: application/json' \
-d '{
"contents": [{
"parts":[{
"text": "The quick brown fox jumps over the lazy dog."
}]
}]
}'
예시 (Examples)
핵심 아이디어는 단순해요. Google AI Studio API의 호스트를 http://0.0.0.0:4000/gemini로 바꾸면 돼요. 원래 https://generativelanguage.googleapis.com을 호출하던 것을 http://0.0.0.0:4000/gemini로 바꾸고, 쿼리 키를 key=$GOOGLE_API_KEY 대신 LiteLLM 키(key=anything 또는 가상 키 key=LITELLM_VIRTUAL_KEY)로 바꾸면 됩니다.
예시 1: 토큰 계산 (Counting Tokens)
LiteLLM 프록시 호출
curl http://0.0.0.0:4000/gemini/v1beta/models/gemini-3.8-flash:countTokens?key=anything \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"contents": [{
"parts":[{
"text": "The quick brown fox jumps over the lazy dog."
}],
}],
}'
Google AI Studio 직접 호출 (변환 전)
curl https://generativelanguage.googleapis.com/v1beta/models/gemini-3.8-flash:countTokens?key=$GOOGLE_API_KEY \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"contents": [{
"parts":[{
"text": "The quick brown fox jumps over the lazy dog."
}],
}],
}'
예시 2: 콘텐츠 생성 (Generate Content)
LiteLLM 프록시 호출
curl "http://0.0.0.0:4000/gemini/v1beta/models/gemini-3.8-flash:generateContent?key=anything" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"contents": [{
"parts":[{"text": "Write a story about a magic backpack."}]
}]
}' 2> /dev/null
Google AI Studio 직접 호출 (변환 전)
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-3.8-flash:generateContent?key=$GOOGLE_API_KEY" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"contents": [{
"parts":[{"text": "Write a story about a magic backpack."}]
}]
}' 2> /dev/null
예시 3: 캐싱 (Caching)
LiteLLM 프록시 호출
curl -X POST "http://0.0.0.0:4000/gemini/v1beta/models/gemini-3.8-flash:generateContent?key=anything" \
-H 'Content-Type: application/json' \
-d '{
"contents": [
{
"parts":[{
"text": "Please summarize this transcript"
}],
"role": "user"
},
],
"cachedContent": "'$CACHE_NAME'"
}'
Google AI Studio 직접 호출 (변환 전)
curl -X POST "https://generativelanguage.googleapis.com/v1beta/models/gemini-3.8-flash:generateContent?key=$GOOGLE_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
"contents": [
{
"parts":[{
"text": "Please summarize this transcript"
}],
"role": "user"
},
],
"cachedContent": "'$CACHE_NAME'"
}'
예시 4: Veo로 비디오 생성하기
Veo를 통한 비디오 생성에 대한 자세한 내용은 Veo 비디오 생성 문서를 참고해 주세요.
고급 (Advanced)
가상 키(Virtual Keys)와 함께 사용하기
가상 키는 LiteLLM 프록시에 데이터베이스가 설정된 경우에 사용할 수 있어요. 가상 키 설정 문서를 참고해 주세요.
환경 변수를 설정해요.
export DATABASE_URL=""
export LITELLM_MASTER_KEY=""
export GEMINI_API_KEY=""
프록시를 실행해요.
litellm
# RUNNING on http://0.0.0.0:4000
가상 키를 생성해요.
curl -X POST 'http://0.0.0.0:4000/key/generate' \
-H "Authorization: Bearer ***" \
-H 'Content-Type: application/json' \
-d '{}'
응답에서 키를 받아요.
{
...
"key": "sk-<virtual-key>"
}
이제 가상 키로 호출해요.
curl http://0.0.0.0:4000/gemini/v1beta/models/gemini-3.8-flash:countTokens?key=sk-<virtual-key>' \
-H 'Content-Type: application/json' \
-d '{
"contents": [{
"parts":[{
"text": "The quick brown fox jumps over the lazy dog."
}]
}]
}'
요청 헤더로 태그(tags) 보내기
요청에 tags 헤더를 넣어 비용/사용량에 태그를 붙일 수 있어요.
tags: ["gemini-js-sdk", "pass-through-endpoint"]
curl 예시
curl 'http://0.0.0.0:4000/gemini/v1beta/models/gemini-3.8-flash:generateContent?key=sk-anything' \
-H 'Content-Type: application/json' \
-H 'tags: gemini-js-sdk,pass-through-endpoint' \
-d '{
"contents": [{
"parts":[{
"text": "The quick brown fox jumps over the lazy dog."
}]
}]
}'
JS SDK 예시
const { GoogleGenAI } = require("@google/genai");
const ai = new GoogleGenAI({
apiKey: "sk-<y...y>",
httpOptions: {
baseUrl: "http://localhost:4000/gemini", // http://<proxy-base-url>/gemini
headers: {
"tags": "gemini-js-sdk,pass-through-endpoint",
},
},
});
async function main() {
try {
const response = await ai.models.generateContent({
model: "gemini-3.8-flash",
contents: "Explain how AI works",
});
console.log(response.text);
} catch (error) {
console.error('Error:', error);
}
}
main();