xAI

xAI

LiteLLM에서 xAI의 모든 모델(Grok 포함)을 사용하는 방법을 알아봐요.

출처: 문서

본문

: 모든 xAI 모델을 지원해요. litellm 요청을 보낼 때 model=xai/을 접두사로 붙이면 돼요.

지원 모델

Grok 4.5 - 코딩, 에이전트 작업, 지식 작업을 위한 프론티어 모델로, 500K 컨텍스트, 추론(low/medium/high), 비전, 도구, 웹 검색, 프롬프트 캐싱을 지원해요.

모델 컨텍스트 기능
xai/grok-4.5 500K 토큰 Reasoning, Function calling, Vision, Web search, Caching

예시:

from litellm import completion

response = completion(
    model="xai/grok-4.5",
    messages=[{"role": "user", "content": "Find and fix the bug, then explain it."}],
    reasoning_effort="high",  # low | medium | high (default high)
)

기능:

  • Reasoning = 추론 토큰을 사용한 CoT(chain-of-thought) 추론
  • Tools = 함수 호출 / 도구 사용
  • Web search = 실시간 인터넷 검색
  • Vision = 이미지 이해
  • Caching = 비용 절감을 위한 프롬프트 캐싱
  • Structured outputs = JSON / 스키마 제약 응답

가격: 현재 요금은 xAI 가격 페이지를 참고해요.

API 키

# env variable
os.environ['XAI_API_KEY']

사용 예시

LiteLLM python sdk 사용법 - 비스트리밍:

from litellm import completion
import os

os.environ['XAI_API_KEY'] = ""
response = completion(
    model="xai/grok-4.5",
    messages=[
        {
            "role": "user",
            "content": "What's the weather like in Boston today in Fahrenheit?",
        }
    ],
    max_tokens=10,
    response_format={ "type": "json_object" },
    seed=123,
    temperature=0.2,
    top_p=0.9,
    tool_choice="auto",
    tools=[],
    user="user",
)
print(response)

사용 예시 - 스트리밍

LiteLLM python sdk 사용법 - 스트리밍:

from litellm import completion
import os

os.environ['XAI_API_KEY'] = ""
response = completion(
    model="xai/grok-4.5",
    messages=[
        {
            "role": "user",
            "content": "What's the weather like in Boston today in Fahrenheit?",
        }
    ],
    stream=True,
    max_tokens=10,
    response_format={ "type": "json_object" },
    seed=123,
    temperature=0.2,
    top_p=0.9,
    tool_choice="auto",
    tools=[],
    user="user",
)

for chunk in response:
    print(chunk)

사용 예시 - 비전

LiteLLM python sdk 사용법 - 비전:

import os
from litellm import completion

os.environ["XAI_API_KEY"] = "your-api-key"

response = completion(
    model="xai/grok-4.5",
    messages=[
        {
            "role": "user",
            "content": [
                {
                    "type": "image_url",
                    "image_url": {
                        "url": "https://science.nasa.gov/wp-content/uploads/2023/09/web-first-images-release.png",
                        "detail": "high",
                    },
                },
                {
                    "type": "text",
                    "text": "What's in this image?",
                },
            ],
        },
    ],
)

LiteLLM Proxy 서버 사용법

config.yaml 수정:

model_list:
  - model_name: my-model
    litellm_params:
      model: xai/  # add xai/ prefix to route as XAI provider
      api_key: api-key                 # api key to send your model

Proxy 시작:

$ litellm --config /path/to/config.yaml

OpenAI Python SDK로 요청:

import openai
client = openai.OpenAI(
    api_key="sk-",             # pass litellm proxy key, if you're using virtual keys
    base_url="http://0.0.0.0:4000" # litellm-proxy-base url
)

response = client.chat.completions.create(
    model="my-model",
    messages = [
        {
            "role": "user",
            "content": "what llm are you"
        }
    ],
)

print(response)
curl --location 'http://0.0.0.0:4000/chat/completions' \
    --header "Authorization: Bearer ***" \
    --header 'Content-Type: application/json' \
    --data '{
    "model": "my-model",
    "messages": [
        {
        "role": "user",
        "content": "what llm are you"
        }
    ],
}'

Reasoning 사용법

LiteLLM은 xAI 모델에 대한 reasoning 사용을 지원해요.

import litellm
response = litellm.completion(
    model="xai/grok-4.5",
    messages=[{"role": "user", "content": "What is 101*3?"}],
    reasoning_effort="low",  # low | medium | high
)

print("Reasoning Content:")
print(response.choices[0].message.reasoning_content)

print("\nFinal Response:")
print(response.choices[0].message.content)

print("\nNumber of completion tokens:")
print(response.usage.completion_tokens)

print("\nNumber of reasoning tokens:")
print(response.usage.completion_tokens_details.reasoning_tokens)

Proxy에서 OpenAI SDK로:

import openai
client = openai.OpenAI(
    api_key="sk-",             # pass litellm proxy key, if you're using virtual keys
    base_url="http://0.0.0.0:4000" # litellm-proxy-base url
)

response = client.chat.completions.create(
    model="xai/grok-4.5",
    messages=[{"role": "user", "content": "What is 101*3?"}],
    reasoning_effort="low",  # low | medium | high
)

print("Reasoning Content:")
print(response.choices[0].message.reasoning_content)

print("\nFinal Response:")
print(response.choices[0].message.content)

print("\nNumber of completion tokens:")
print(response.usage.completion_tokens)

print("\nNumber of reasoning tokens:")
print(response.usage.completion_tokens_details.reasoning_tokens)

예시 응답:

Reasoning Content:
Let me calculate 101 multiplied by 3:
101 * 3 = 303.
I can double-check that: 100 * 3 is 300, and 1 * 3 is 3, so 300 + 3 = 303. Yes, that's correct.

Final Response:
The result of 101 multiplied by 3 is 303.

Number of completion tokens:
14

Number of reasoning tokens:
310

더 알아보기 (Learn more)

  • xAI 공식 문서
  • xAI 가격 페이지