Skip to main content
관찰 가능성은 프로덕션 환경에서 에이전트가 어떻게 동작하는지 이해하는 데 매우 중요합니다. LangChain의 @[create_agent]를 사용하면 LangSmith를 통해 내장된 관찰 가능성을 제공받을 수 있습니다. LangSmith는 LLM 애플리케이션을 추적, 디버깅, 평가, 모니터링할 수 있는 강력한 플랫폼입니다. 트레이스는 초기 사용자 입력부터 최종 응답까지 에이전트가 수행하는 모든 단계를 캡처합니다. 여기에는 모든 도구 호출, 모델 상호작용, 의사 결정 지점이 포함됩니다. 이를 통해 에이전트를 디버깅하고, 성능을 평가하며, 사용량을 모니터링할 수 있습니다.

사전 준비 사항

시작하기 전에 다음 사항을 준비해야 합니다:

트레이싱 활성화

모든 LangChain 에이전트는 자동으로 LangSmith 트레이싱을 지원합니다. 트레이싱을 활성화하려면 다음 환경 변수를 설정하세요:
export LANGSMITH_TRACING=true
export LANGSMITH_API_KEY=<your-api-key>
API 키는 LangSmith 설정에서 확인할 수 있습니다.

빠른 시작

LangSmith에 트레이스를 기록하기 위해 추가 코드가 필요하지 않습니다. 평소처럼 에이전트 코드를 실행하기만 하면 됩니다:
import { createAgent } from "@langchain/agents";

function sendEmail(to: string, subject: string, body: string): string {
    // ... 이메일 전송 로직
    return `Email sent to ${to}`;
}

function searchWeb(query: string): string {
    // ... 웹 검색 로직
    return `Search results for: ${query}`;
}

const agent = createAgent({
    model: "openai:gpt-4o",
    tools: [sendEmail, searchWeb],
    systemPrompt: "You are a helpful assistant that can send emails and search the web."
});

// 에이전트 실행 - 모든 단계가 자동으로 추적됩니다
const response = await agent.invoke({
    messages: [{ role: "user", content: "Search for the latest AI news and email a summary to [email protected]" }]
});
기본적으로 트레이스는 default라는 이름의 프로젝트에 기록됩니다. 사용자 정의 프로젝트 이름을 구성하려면 프로젝트에 로그 기록을 참조하세요.

선택적 트레이싱

LangSmith의 tracing_context 컨텍스트 매니저를 사용하여 특정 호출이나 애플리케이션의 일부만 트레이싱하도록 선택할 수 있습니다:
import langsmith as ls

# 이것은 트레이싱됩니다
with ls.tracing_context(enabled=True):
    agent.invoke({"messages": [{"role": "user", "content": "Send a test email to [email protected]"}]})

# 이것은 트레이싱되지 않습니다 (LANGSMITH_TRACING이 설정되지 않은 경우)
agent.invoke({"messages": [{"role": "user", "content": "Send another email"}]})

프로젝트에 로깅

전체 애플리케이션에 대한 사용자 정의 프로젝트 이름을 LANGSMITH_PROJECT 환경 변수를 설정하여 지정할 수 있습니다:
export LANGSMITH_PROJECT=my-agent-project
특정 작업에 대해 프로그래밍 방식으로 프로젝트 이름을 설정할 수 있습니다:
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"}]
    })

트레이스에 메타데이터 추가

사용자 정의 메타데이터와 태그로 트레이스에 주석을 달 수 있습니다:
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 문서를 참조하세요.

Connect these docs programmatically to Claude, VSCode, and more via MCP for real-time answers.
I