Observability
Observability는 프로덕션 환경에서 에이전트가 어떻게 동작하는지 이해하는 데 매우 중요합니다. LangChain의 create_agent를 사용하면 LangSmith를 통한 내장 관찰 기능을 사용할 수 있습니다. LangSmith는 LLM 애플리케이션을 추적(tracing), 디버깅, 평가 및 모니터링하기 위한 강력한 플랫폼입니다.
트레이스는 초기 사용자 입력부터 최종 응답까지 에이전트가 수행하는 모든 단계를 캡처하며, 모든 도구 호출, 모델 상호작용 및 의사 결정 지점을 포함합니다. 이를 통해 에이전트를 디버깅하고, 성능을 평가하며, 사용량을 모니터링할 수 있습니다.
Prerequisites
시작하기 전에 다음 사항을 확인하세요:
- LangSmith 계정 (무료 가입 가능)
Enable tracing
모든 LangChain 에이전트는 자동으로 LangSmith 추적을 지원합니다. 이를 활성화하려면 다음 환경 변수를 설정하세요:
export LANGSMITH_TRACING=true
export LANGSMITH_API_KEY=<your-api-key>
API 키는 LangSmith 설정에서 얻을 수 있습니다.
Quick start
LangSmith에 트레이스를 기록하기 위해 추가 코드가 필요하지 않습니다. 평소처럼 에이전트 코드를 실행하기만 하면 됩니다:
from langchain.agents import create_agent
def send_email(to: str, subject: str, body: str):
"""Send an email to a recipient."""
# ... email sending logic
return f"Email sent to {to}"
def search_web(query: str):
"""Search the web for information."""
# ... web search logic
return f"Search results for: {query}"
agent = create_agent(
model="gpt-4o",
tools=[send_email, search_web],
system_prompt="You are a helpful assistant that can send emails and search the web."
)
# Run the agent - all steps will be traced automatically
response = agent.invoke({
"messages": [{
"role": "user",
"content": "Search for the latest AI news and email a summary to john@example.com"
}]
})
기본적으로 트레이스는 default라는 이름의 프로젝트에 기록됩니다. 사용자 지정 프로젝트 이름을 구성하려면 Log to a project를 참조하세요.
Trace selectively
LangSmith의 tracing_context 컨텍스트 매니저를 사용하여 특정 호출이나 애플리케이션의 일부만 선택적으로 추적할 수 있습니다:
import langsmith as ls
# This WILL be traced
with ls.tracing_context(enabled=True):
agent.invoke({
"messages": [{
"role": "user",
"content": "Send a test email to alice@example.com"
}]
})
# This will NOT be traced (if LANGSMITH_TRACING is not set)
agent.invoke({
"messages": [{
"role": "user",
"content": "Send another email"
}]
})
Log to a project
Statically
LANGSMITH_PROJECT 환경 변수를 설정하여 전체 애플리케이션에 대한 사용자 지정 프로젝트 이름을 설정할 수 있습니다:
export LANGSMITH_PROJECT=my-agent-project
Dynamically
특정 작업에 대해 프로그래밍 방식으로 프로젝트 이름을 설정할 수 있습니다:
import langsmith as ls
with ls.tracing_context(project_name="email-agent-test", enabled=True):
response = agent.invoke({
"messages": [{
"role": "user",
"content": "Send a welcome email"
}]
})
Add metadata to traces
사용자 지정 메타데이터 및 태그로 트레이스에 주석을 달 수 있습니다:
response = agent.invoke(
{
"messages": [{
"role": "user",
"content": "Send a welcome email"
}]
},
config={
"tags": ["production", "email-assistant", "v1.0"],
"metadata": {
"user_id": "user_123",
"session_id": "session_456",
"environment": "production"
}
}
)
tracing_context는 세밀한 제어를 위해 태그와 메타데이터도 허용합니다:
with ls.tracing_context(
project_name="email-agent-test",
enabled=True,
tags=["production", "email-assistant", "v1.0"],
metadata={
"user_id": "user_123",
"session_id": "session_456",
"environment": "production"
}
):
response = agent.invoke(
{
"messages": [{
"role": "user",
"content": "Send a welcome email"
}]
}
)
이 사용자 지정 메타데이터 및 태그는 LangSmith의 트레이스에 첨부됩니다.
트레이스를 사용하여 에이전트를 디버깅, 평가 및 모니터링하는 방법에 대해 자세히 알아보려면 LangSmith 문서를 참조하세요.
출처: https://docs.langchain.com/oss/python/langchain/observability
Langchain v1.0
- LangChain 개요
- LangChain v1
- LangChain v1 마이그레이션 가이드
- LangChain 설치
- QuickStart
- Philosophy
- Agents
- Models
- Messages
- Tools
- Short-term memory
- Streaming
- Middleware
- Structured output
- Guardrails
- Runtime
- Context Engineering
- Model Context Protocol (MCP)
- Human-in-the-loop
- Multi-agent
- Retrieval
- Long-term memory
- Studio
- Test
- Deploy
- Agent Chat UI
Observability
