청구

청구 (Billing)

내부 팀, 외부 고객의 사용량에 대해 요금을 청구하는 방법을 알려드려요.

출처: 문서

본문

🚨 요구사항

사용량 기반 청구를 위해 Lago를 설정하세요. Stripe 튜토리얼을 따라하는 걸 권장해요.

단계:

  1. 프록시를 Lago에 연결하기
  2. 청구할 id 설정하기 (customers, internal users, teams)
  3. 시작!

빠른 시작 (Quick Start)

내부 팀의 사용량에 대해 요금을 청구해 봐요.

1. 프록시를 Lago에 연결하기

프록시 config.yaml'lago'를 콜백으로 설정하세요.

model_list:
  - model_name: fake-openai-endpoint
    litellm_params:
      model: openai/fake
      api_key: fake-key
      api_base: https://exampleopenaiendpoint-production.up.railway.app/
litellm_settings:
  callbacks: ["lago"] # 👈 KEY CHANGE
general_settings:
  master_key: os.environ/LITELLM_MASTER_KEY

환경에 Lago 키를 추가하세요.

export LAGO_API_BASE="http://localhost:3000" # self-host - https://docs.getlago.com/guide/self-hosted/docker#run-the-app
export LAGO_API_KEY="3e29d607-de54-49aa-a019-ecf585729070" # Get key - https://docs.getlago.com/guide/self-hosted/docker#find-your-api-key
export LAGO_API_EVENT_CODE="openai_tokens" # name of lago billing code
export LAGO_API_CHARGE_BY="team_id" # 👈 Charges 'team_id' attached to proxy key

프록시 시작:

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

2. 내부 팀용 키 생성하기 (Create Key for Internal Team)

curl 'http://0.0.0.0:4000/key/generate' \
--header "Authorization: Bearer ***" \
--header 'Content-Type: application/json' \
--data-raw '{"team_id": "my-unique-id"}' # 👈 Internal Team's ID

응답 객체:

{
  "key": "«redacted:sk-…»",
}

3. 청구 시작! (Start billing!)

Curl

# Authorization: *** Team's Key
curl --location 'http://0.0.0.0:4000/chat/completions' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer ***' \
--data ' {
      "model": "fake-openai-endpoint",
      "messages": [
        {
          "role": "user",
          "content": "what llm are you"
        }
      ],
    }'

OpenAI Python SDK

import openai
client = openai.OpenAI(
    api_key="«redacted:sk-…»", # 👈 Team's Key
    base_url="http://0.0.0.0:4000")
# litellm 프록시에 설정된 모델로 요청 전송, `litellm --model`
response = client.chat.completions.create(
    model="gpt-5.6-terra",
    messages = [
        {
            "role": "user",
            "content": "this is a test request, write a short poem"
        }
    ])
print(response)

Langchain

from langchain.chat_models import ChatOpenAI
from langchain.prompts.chat import (
    ChatPromptTemplate,
    HumanMessagePromptTemplate,
    SystemMessagePromptTemplate,
)
from langchain.schema import HumanMessage, SystemMessage
import os

os.environ["OPENAI_API_KEY"] = "«redacted:sk-…»" # 👈 Team's Key
chat = ChatOpenAI(
    openai_api_base="http://0.0.0.0:4000",
    model = "gpt-5.6-terra",
    temperature=0.1,
)
messages = [
    SystemMessage(
        content="You are a helpful assistant that im using to make a test request to."
    ),
    HumanMessage(
        content="test from litellm. tell me why it's amazing in 1 sentence"
    ),
]
response = chat(messages)
print(response)

Lago에서 결과 확인하기.

고급 - Lago 로깅 객체 (Advanced - Lago Logging object)

LiteLLM이 Lago에 로깅하는 내용이에요:

{
    "event": {
      "transaction_id": "<generated_unique_id>",
      "external_customer_id": <selected_id>, # either 'end_user_id', 'user_id', or 'team_id'. Default 'end_user_id'.
      "code": os.getenv("LAGO_API_EVENT_CODE"),
      "properties": {
          "input_tokens": <number>,
          "output_tokens": <number>,
          "model": <string>,
          "response_cost": <number>, # 👈 LITELLM CALCULATED RESPONSE COST - https://github.com/BerriAI/litellm/blob/d43f75150a65f91f60dc2c0c9462ce3ffc713c1f/litellm/utils.py#L1473
      }
    }
}

고급 - 고객, 내부 사용자 청구 (Advanced - Bill Customers, Internal Users)

다음에 대해:

  • Customers (/chat/completion 호출의 'user' 파라미터로 전달되는 id) = 'end_user_id'
  • Internal Users (키 생성 시 설정되는 id) = 'user_id'
  • Teams (키 생성 시 설정되는 id) = 'team_id'

고객 청구 (Customer Billing)

'LAGO_API_CHARGE_BY''end_user_id'로 설정:

export LAGO_API_CHARGE_BY="end_user_id"

테스트!

curl --location 'http://0.0.0.0:4000/chat/completions' \
--header 'Content-Type: application/json' \
--data ' {
      "model": "gpt-5.6-terra",
      "messages": [
        {
          "role": "user",
          "content": "what llm are you"
        }
      ],
      "user": "my_customer_id" # 👈 whatever your customer id is
    }'
import openai
client = openai.OpenAI(
    api_key="anything",
    base_url="http://0.0.0.0:4000")
# litellm 프록시에 설정된 모델로 요청 전송, `litellm --model`
response = client.chat.completions.create(
    model="gpt-5.6-terra",
    messages = [
        {
            "role": "user",
            "content": "this is a test request, write a short poem"
        }
    ],
    user="my_customer_id") # 👈 whatever your customer id is
print(response)
from langchain.chat_models import ChatOpenAI
from langchain.prompts.chat import (
    ChatPromptTemplate,
    HumanMessagePromptTemplate,
    SystemMessagePromptTemplate,
)
from langchain.schema import HumanMessage, SystemMessage
import os

os.environ["OPENAI_API_KEY"] = "anything"
chat = ChatOpenAI(
    openai_api_base="http://0.0.0.0:4000",
    model = "gpt-5.6-terra",
    temperature=0.1,
    extra_body={
        "user": "my_customer_id"  # 👈 whatever your customer id is
    })
messages = [
    SystemMessage(
        content="You are a helpful assistant that im using to make a test request to."
    ),
    HumanMessage(
        content="test from litellm. tell me why it's amazing in 1 sentence"
    ),
]
response = chat(messages)
print(response)

내부 사용자 청구 (Internal User Billing)

'LAGO_API_CHARGE_BY''user_id'로 설정:

export LAGO_API_CHARGE_BY="user_id"

그 사용자용 키 생성:

curl 'http://0.0.0.0:4000/key/generate' \
--header 'Authorization: Bearer ***' \
--header 'Content-Type: application/json' \
--data-raw '{"user_id": "my-unique-id"}' # 👈 Internal User's id

응답 객체:

{
  "key": "«redacted:sk-…»",
}

그 키로 API 호출:

import openai
client = openai.OpenAI(
    api_key="«redacted:sk-…»", # 👈 Generated key
    base_url="http://0.0.0.0:4000")
# litellm 프록시에 설정된 모델로 요청 전송, `litellm --model`
response = client.chat.completions.create(
    model="gpt-5.6-terra",
    messages = [
        {
            "role": "user",
            "content": "this is a test request, write a short poem"
        }
    ])
print(response)