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

사전 요구사항

시작하기 전에 다음 사항을 확인하세요:

tracing 활성화

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

빠른 시작

LangSmith에 trace를 기록하기 위해 추가 코드가 필요하지 않습니다. 평소처럼 에이전트 코드를 실행하기만 하면 됩니다:
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="openai: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 [email protected]"}]
})
기본적으로 trace는 default라는 이름의 프로젝트에 기록됩니다. 사용자 정의 프로젝트 이름을 구성하려면 프로젝트에 로그 기록하기를 참조하세요.

선택적으로 추적하기

LangSmith의 tracing_context context manager를 사용하여 특정 호출이나 애플리케이션의 일부를 추적하도록 선택할 수 있습니다:
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 [email protected]"}]})

# This will NOT be traced (if LANGSMITH_TRACING is not set)
agent.invoke({"messages": [{"role": "user", "content": "Send another email"}]})

project에 로그 기록하기

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

trace에 metadata 추가하기

사용자 정의 metadata와 tag로 trace에 주석을 달 수 있습니다:
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는 세밀한 제어를 위해 tag와 metadata도 허용합니다:
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"}]}
    )
이 사용자 정의 metadata와 tag는 LangSmith의 trace에 첨부됩니다.
agent를 디버그, 평가 및 모니터링하기 위해 trace를 사용하는 방법에 대해 자세히 알아보려면 LangSmith 문서를 참조하세요.

---

<Callout icon="pen-to-square" iconType="regular">
    [Edit the source of this page on GitHub.](https://github.com/langchain-ai/docs/edit/main/src/oss/langchain/observability.mdx)
</Callout>
<Tip icon="terminal" iconType="regular">
    [Connect these docs programmatically](/use-these-docs) to Claude, VSCode, and more via MCP for    real-time answers.
</Tip>
I