작업 기록
작업 기록 (Recording Operations)
에이전트 애플리케이션에서 작업과 LLM 호출을 추적하는 방법을 소개해요. 기본 설정부터 자동 계측, 그리고 더 상세한 계측을 위한 데코레이터까지 알아볼게요.
출처: 문서
본문
AgentOps는 최소한의 설정으로 AI 애플리케이션의 작업과 상호작용을 쉽게 추적할 수 있게 해줍니다.
기본 설정 (Basic Setup)
AgentOps를 시작하는 가장 간단한 방법은 애플리케이션 시작 시 초기화하는 것입니다.
import agentops
# Initialize AgentOps with your API key
agentops.init("your-api-key")
바로 그겁니다! 이 한 줄의 코드로 다음을 수행합니다.
- 애플리케이션 실행 추적을 위한 세션을 자동으로 생성
- 지원되는 프로바이더(OpenAI, Anthropic 등)에 대한 모든 LLM 호출을 가로채 추적
- 토큰 수, 비용, 응답 시간 같은 관련 메트릭을 기록
초기화 시 커스텀 트레이스 이름을 설정할 수도 있습니다.
import agentops
# Initialize with custom trace name
agentops.init("your-api-key", trace_name="my-custom-workflow")
자동 계측 (Automatic Instrumentation)
AgentOps는 추가 코드 없이 인기 있는 LLM 프로바이더에 대한 호출을 자동으로 계측합니다.
import agentops
from openai import OpenAI
# Initialize AgentOps
agentops.init("your-api-key")
# Make LLM calls as usual - AgentOps will track them automatically
client = OpenAI()
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Hello, world!"}]
)
이는 다음을 포함한 많은 인기 LLM 프로바이더에서 동작합니다.
- OpenAI
- Anthropic
- Google (Gemini)
- Cohere
- 그 외 더
고급: 상세 계측을 위한 데코레이터 사용 (Advanced: Using Decorators for Detailed Instrumentation)
더 상세한 추적을 위해 AgentOps는 코드를 명시적으로 계측하는 데코레이터를 제공합니다. 이는 선택 사항이지만 대시보드에서 더 많은 컨텍스트를 제공할 수 있어요.
@operation 데코레이터
@operation 데코레이터는 애플리케이션의 특정 작업을 추적하는 데 도움을 줍니다.
from agentops.sdk.decorators import operation
@operation
def process_data(data):
# Process the data
return result
@agent 데코레이터
에이전트 클래스를 사용한다면 @agent 데코레이터로 추적할 수 있어요.
from agentops.sdk.decorators import agent, operation
@agent
class ResearchAgent:
@operation
def search(self, query):
# Implementation of search
return f"Results for: {query}"
def research_workflow(topic):
agent = ResearchAgent()
results = agent.search(topic)
return results
results = research_workflow("quantum computing")
@tool 데코레이터
@tool 데코레이터로 도구 사용과 비용을 추적하세요. 비용을 지정하면 대시보드 요약에서 직접 총 비용 추적을 얻을 수 있습니다.
from agentops.sdk.decorators import tool
@tool(cost=0.05)
def web_search(query):
# Tool implementation
return f"Search results for: {query}"
@tool
def calculator(expression):
# Tool without cost tracking
return eval(expression)
@trace 데코레이터
관련 작업을 그룹화하는 커스텀 트레이스를 @trace 데코레이터로 만드세요. 이는 대부분의 애플리케이션에서 권장되는 방법입니다.
import agentops
from agentops.sdk.decorators import trace, agent, operation
# Initialize AgentOps without auto-starting session since we use @trace
agentops.init("your-api-key", auto_start_session=False)
@trace(name="customer-service-workflow", tags=["customer-support"])
def customer_service_workflow(customer_id):
agent = ResearchAgent()
results = agent.search(f"customer {customer_id}")
return results
모범 사례 (Best Practices)
-
간단하게 유지하세요: 대부분의 애플리케이션에서는
agentops.init()으로 AgentOps를 초기화하는 것으로 충분합니다. -
커스텀 워크플로우에는 @trace 사용: 작업을 그룹화해야 할 때는 수동 트레이스 관리 대신
@trace데코레이터를 사용하세요. -
의미 있는 이름과 태그: 데코레이터를 사용할 때 대시보드에서 식별하기 쉽도록 설명적인 이름과 관련 태그를 선택하세요.
-
비용 추적:
@tool데코레이터를 비용 파라미터와 함께 사용해 대시보드에서 도구 사용 비용을 추적하세요.