Literal AI

Literal AI

Literal AI는 프로덕션급 LLM 앱을 구축하기 위한 협업 관측성·평가·분석 플랫폼이에요. LiteLLM 통화를 Literal AI로 로깅하고 트레이스를 연결하는 방법을 알려드릴게요.

출처: 문서

본문

사전 요구사항 (Pre-Requisites)

literalai 패키지가 설치되어 있어야 해요.

uv add literalai litellm

빠른 시작 (Quick Start)

import litellm
import os

os.environ["LITERAL_API_KEY"] = ""
os.environ['OPENAI_API_KEY']= ""
os.environ['LITERAL_BATCH_SIZE'] = "1" # You won't see logs appear until the batch is full and sent

litellm.success_callback = ["literalai"] # Log Input/Output to LiteralAI
litellm.failure_callback = ["literalai"] # Log Errors to LiteralAI

# openai call
response = litellm.completion(
  model="gpt-5.6-luna",
  messages=[
    {"role": "user", "content": "Hi 👋 - i'm openai"}
  ]
)

다중 단계 트레이스 (Multi Step Traces)

이 통합은 Literal AI SDK 데코레이터와 호환되어 대화·에이전트 트레이스를 지원해요.

import litellm
from literalai import LiteralClient
import os

os.environ["LITERAL_API_KEY"] = ""
os.environ['OPENAI_API_KEY']= ""
os.environ['LITERAL_BATCH_SIZE'] = "1" # You won't see logs appear until the batch is full and sent

litellm.input_callback = ["literalai"] # Support other Literal AI decorators and prompt templates
litellm.success_callback = ["literalai"] # Log Input/Output to LiteralAI
litellm.failure_callback = ["literalai"] # Log Errors to LiteralAI

literalai_client = LiteralClient()

@literalai_client.run
def my_agent(question: str):
    # agent logic here
    response = litellm.completion(
        model="gpt-5.6-luna",
        messages=[
            {"role": "user", "content": question}
        ],
        metadata={"literalai_parent_id": literalai_client.get_current_step().id}
    )
    return response

my_agent("Hello world")

# Waiting to send all logs before exiting, not needed in a production server
literalai_client.flush()

Literal AI 로깅 기능에 대해 자세히 알아봐요.

프롬프트 템플릿에 생성 연결 (Bind a Generation to its Prompt Template)

이 통합은 Literal AI에서 관리하는 프롬프트와 즉시(Out of the box) 작동해요. 즉 특정 LLM 생성이 해당 템플릿에 바인딩돼요.

Literal AI의 프롬프트 관리에 대해 자세히 알아봐요.

OpenAI 프록시 사용 (OpenAI Proxy Usage)

LiteLLM 프록시를 사용한다면 Literal AI OpenAI 계측(instrumentation)을 이용해 호출을 로깅할 수 있어요.

from literalai import LiteralClient
from openai import OpenAI

client = OpenAI(
    api_key="anything",            # litellm proxy virtual key
    base_url="http://0.0.0.0:4000" # litellm proxy base_url
)

literalai_client = LiteralClient(api_key="")

# Instrument the OpenAI client
literalai_client.instrument_openai()

settings = {
    "model": "gpt-5.6-luna", # model you want to send litellm proxy
    "temperature": 0,
    # ... more settings
}

response = client.chat.completions.create(
        messages=[
            {
                "content": "You are a helpful bot, you always reply in Spanish",
                "role": "system"
            },
            {
                "content": message.content,
                "role": "user"
            }
        ],
        **settings
    )

더 알아보기 (Learn more)