Python SDK for AI agent monitoring, LLM cost tracking, benchmarking, and more. Integrates with most LLMs and agent frameworks including CrewAI, Agno, OpenAI Agents SDK, Langchain, Autogen, AG2, and CamelAI
Python agentagentopsagents-sdkaianthropicautogencost-estimationcrewaievalsevaluation-metricsgroqlangchainllmmistralollamaopenaiopenai-agents
Updated decorators and session tags documentations (#841)
Dwijcebfa05db15c
diff --git a/README.md b/README.mdindex e7b2c072..7cda3f30 100644--- a/README.md+++ b/README.md@@ -228,7 +228,7 @@ from camel.models import ModelFactoryfrom camel.types import ModelPlatformType, ModelType# Initialize AgentOps-agentops.init(os.getenv("AGENTOPS_API_KEY"), default_tags=["CAMEL Example"])+agentops.init(os.getenv("AGENTOPS_API_KEY"), tags=["CAMEL Example"])# Import toolkits after AgentOps init for trackingfrom camel.toolkits import SearchToolkitdiff --git a/docs/mint.json b/docs/mint.jsonindex b241b368..0e51d862 100644--- a/docs/mint.json+++ b/docs/mint.json@@ -119,7 +119,7 @@"v1/usage/advanced-configuration","v1/usage/tracking-llm-calls","v1/usage/tracking-agents",- "v1/usage/recording-events",+ "v1/usage/recording-operations","v1/usage/multiple-sessions"]},diff --git a/docs/v1/concepts/core-concepts.mdx b/docs/v1/concepts/core-concepts.mdxindex 31df1f47..4a6cc74c 100644--- a/docs/v1/concepts/core-concepts.mdx+++ b/docs/v1/concepts/core-concepts.mdx@@ -11,7 +11,7 @@ The AgentOps SDK works to provide as much functionality with as little developer- Auto-Instrumenting- After calling `agentops.init()` we automatically look for installed LLM providers and auto-instrument their calls. This allows us to capture calls between your code and the provider to collect data for your dashboard.- Decorators- - With [Decorators](/v1/concepts/decorators), the SDK can add code to your existing functions and classes.+ - With [Decorators](/v1/concepts/decorators), the SDK can add tracing to your existing functions and classes to create hierarchical spans for monitoring and analysis.- Process monitoring- The SDK establishes a couple process monitors that allow us to understand the state and health of your agents.@@ -55,36 +55,53 @@ If multiple sessions exist, you are expected to call the function on the relevan<CodeGroup>```python single sessionimport agentops-agentops.start_session()-agentops.end_session(end_state='Success')+from agentops.sdk.decorators import session++@session+def my_session():+ # Your session code here+ pass++# Run the session+my_session()``````python multi-sessionimport agentops-session_1 = agentops.start_session()-session_2 = agentops.start_session()+from agentops.sdk.decorators import session++@session+def session_1():+ # Session 1 code+ pass++@session+def session_2():+ # Session 2 code+ pass-session_1.end_session(end_state='Success')-session_2.end_session(end_state='Failure')+# Run both sessions+session_1()+session_2()```</CodeGroup>For more documentation on using multiple concurrent sessions, please see [Multiple Sessions](v1/usage/multiple-sessions) and [FastAPI Example](/v1/examples/fastapi).-### LLMs, Tools, and Actions (Events)+### LLMs, Tools, and Operations (Spans)-Within AgentOps, **LLMs**, **Tools**, and **Actions** are categorized as **Events**, executed by Agents. Agents primarily initiate LLM calls, potentially leading to API/Tool calls, while Actions encompass any other significant procedures, such as executing functions, taking screenshots, etc.+Within AgentOps, **LLMs**, **Tools**, and **Operations** are categorized as **Spans**, executed by Agents. Agents primarily initiate LLM calls, potentially leading to API/Tool calls, while Operations encompass any other significant procedures, such as executing functions, taking screenshots, etc.-All events share the following characteristics:+All spans share the following characteristics:- **ID**: A unique identifier.-- **Session ID**: The session to which the event belongs.-- **Agent ID**: Identifies the agent responsible for the event.-- **Parameters**: The inputs provided to the event.-- **Returns**: The outputs or results of the event.-- **Starting Timestamp**: The time at which the event began.-- **Ending Timestamp**: The time at which the event concluded.+- **Session ID**: The session to which the span belongs.+- **Agent ID**: Identifies the agent responsible for the span.+- **Parameters**: The inputs provided to the span.+- **Returns**: The outputs or results of the span.+- **Starting Timestamp**: The time at which the span began.+- **Ending Timestamp**: The time at which the span concluded.-Additionally, each event type has its own specific properties:+Additionally, each span type has its own specific properties:**LLMs**:- **Model**: The specific LLM model used.@@ -92,22 +109,21 @@ Additionally, each event type has its own specific properties:- **Completion Messages**: The responses received from the model.- **Prompt Tokens**: The number of tokens used in the prompts.- **Completion Tokens**: The number of tokens in the model's responses.-- **Cost**: The cost incurred by the event.-- **Thread ID**: Associates the event with a specific thread for execution tracking.+- **Cost**: The cost incurred by the span.+- **Thread ID**: Associates the span with a specific thread for execution tracking.**Tools**:- **Logs**: Records of the tool's operation, including any outputs and errors.-**Actions**:-- **Action Type**: Specifies the nature of the action (e.g., function execution, screenshot).-- **Logs**: Detailed records of the action's execution, including parameters and outcomes.-- **Screenshot**: If applicable, an image capture of the screen at the time of the action.+**Operations**:+- **Operation Type**: Specifies the nature of the operation (e.g., function execution, screenshot).+- **Logs**: Detailed records of the operation's execution, including parameters and outcomes.### Errors-Errors are an inevitable aspect of operational processes. AgentOps comprehensively documents errors related to Events, providing a wealth of information for troubleshooting.+Errors are an inevitable aspect of operational processes. AgentOps comprehensively documents errors related to Spans, providing a wealth of information for troubleshooting.-- **Error Type**: The event type (LLM, Tool, or Action) the error is associated with.+- **Error Type**: The span type (LLM, Tool, or Operation) the error is associated with.- **Error Code**: A specific code identifying the error.- **Details**: A detailed description of the error.- **Logs/Stack Trace**: Logs or stack traces capturing the error context.@@ -125,7 +141,7 @@ Agents are characterized by:Optionally, agents may also have:- **Logs**: Textual records of the tasks performed by the agent.-**Note**: For events not specifically assigned to an agent, AgentOps will automatically create and assign a default agent.+**Note**: For spans not specifically assigned to an agent, AgentOps will automatically create and assign a default agent.### Threadsdiff --git a/docs/v1/concepts/decorators.mdx b/docs/v1/concepts/decorators.mdxindex 01f372e3..bae2d5c4 100644--- a/docs/v1/concepts/decorators.mdx+++ b/docs/v1/concepts/decorators.mdx@@ -11,22 +11,141 @@ description: "Seemingly magic tools that can be added to functions and classes f> ...> ```-## `@track_agent()`-If your implementation uses Classes to denote Agents, this decorator enables automatic agent tracking.+AgentOps provides a set of decorators that allow you to easily instrument your code for tracing and monitoring AI agent workflows. These decorators create spans (units of work) that are organized hierarchically to track different types of operations.-Learn more about tracking agents [here](/v1/usage/tracking-agents).+## `@session`-## `@record_action()`-Sometimes, your agent system uses functions that are important to track as [`Actions`](/v1/concepts/events/#actionevent).+The `@session` decorator creates a session span, which serves as the root for all other spans. No spans can exist without a session at the top.-Adding this decorator above any function will allow every instance of that function call to be tracked and displayed-in your [Session](v1/concepts/sessions) Drill-Down on the dashboard.+```python+from agentops.sdk.decorators import session-## `@record_tool()`-Some functions are used as Tools. If you are not using an agent framework that records [`ToolEvents`](/v1/concepts/events/#toolevent) with AgentOps automatically, this decorator will record `ToolEvents` when the function is called.+@session+def my_workflow():+ # Your session code here+ return result+```-Adding this decorator above any function will allow every instance of that function call to be tracked and displayed-in your [Session](v1/concepts/sessions) Drill-Down on the dashboard.+You can also use the decorator with parameters:++```python+@session(name="custom-session-name", version=1)+def my_workflow():+ # Your session code here+ return result+```++## `@agent`++The `@agent` decorator creates an agent span for tracking agent operations. Agent spans are typically children of session spans and parents of operation spans.++```python+from agentops.sdk.decorators import agent++@agent+class MyAgent:+ def __init__(self, name):+ self.name = name++ # Agent methods here+```++You can also specify a custom name for the agent:++```python+@agent(name="research-assistant")+class MyAgent:+ # Agent implementation+```++## `@operation` / `@task`++The `@operation` and `@task` decorators are aliases that create operation/task spans for tracking specific operations. These spans are typically children of agent spans.++```python+from agentops.sdk.decorators import agent, operation++@agent+class MyAgent:+ @operation+ def perform_task(self, task):+ # Operation implementation+ return result+```++Operations can also be used outside of agent classes:++```python+from agentops.sdk.decorators import operation++@operation+def process_data(data):+ # Process the data+ return result+```++## `@workflow`++The `@workflow` decorator creates workflow spans for tracking workflows, which can contain multiple operations.++```python+from agentops.sdk.decorators import workflow++@workflow+def my_workflow(data):+ # Workflow implementation+ return result+```++## Nesting and Hierarchy++The decorators automatically manage the context propagation, ensuring that spans are properly nested within their parent spans. The typical hierarchy is:++1. Session (root)+2. Agent+3. Operation/Task+4. Nested Operations++Example of proper nesting:++```python+from agentops.sdk.decorators import session, agent, operation++@agent+class MyAgent:+ @operation+ def nested_operation(self, message):+ return f"Processed: {message}"++ @operation+ def main_operation(self):+ result = self.nested_operation("test message")+ return result++@session+def my_session():+ agent = MyAgent()+ return agent.main_operation()++# Run the session+result = my_session()+```++## Additional Features++The decorators provide several additional features:++1. **Input/Output Recording**: The decorators automatically record the input arguments and output results of the decorated functions.++2. **Exception Handling**: If an exception occurs within a decorated function, it's recorded in the span.++3. **Support for Different Function Types**: The decorators handle different types of functions:+ - Regular synchronous functions+ - Asynchronous functions (using `async/await`)+ - Generator functions (using `yield`)+ - Asynchronous generator functions (using `async` and `yield`)++4. **Custom Attributes**: You can add custom attributes to spans using the decorator parameters.<script type="module" src="/scripts/github_stars.js"></script><script type="module" src="/scripts/adjust_api_dynamically.js"></script>\ No newline at end of filediff --git a/docs/v1/concepts/events.mdx b/docs/v1/concepts/events.mdxindex cb167ad3..fb1fad28 100644--- a/docs/v1/concepts/events.mdx+++ b/docs/v1/concepts/events.mdx@@ -1,110 +1,118 @@----title: "Events"-description: "AgentOps currently supports four event types: ActionEvent, LLMEvent, ToolEvent, and ErrorEvent."+title: "Spans"+description: "AgentOps uses spans to track different types of operations in your agent workflows."----## Event Class+## Spans Overview-The Event class is an abstract base class for events that will be recorded. It should not be instantiated directly.+In AgentOps v0.4, the concept of "Events" has been replaced with "Spans" for all operation tracking. Spans represent different types of operations and are organized hierarchically.-| Parameter | Type | Default | Example | Description |-| -------------- | ---- | ---------------------------- | ------------------------------------ | ----------------------------------------------------------- |-| params | dict | None | `{"param1": "value1"}` | Additional parameters for the event |-| returns | str | None | "Success" | Return value of the event |-| init_timestamp | str | A timestamp of instantiation | "2023-03-15T18:59:13Z" | The time when the Event was initiated |-| end_timestamp | str | A timestamp of instantiation | "2023-03-15T19:00:13Z" | The time when the Event was completed |-| agent_id | UUID | UUID of the triggering agent | 123e4567-e89b-12d3-a456-426614174000 | The unique identifier of the agent that triggered the event |-| id | UUID | A new UUID | 123e4567-e89b-12d3-a456-426614170000 | The unique identifier of the event |+A span is a unit of work or operation in your agent workflow. Spans are created using decorators and are automatically nested to create a hierarchical trace of your agent's execution.-## ActionEvent+## Span Types-The `ActionEvent` is a generic event for recording events that do not fit into the bounds of a more specific Event type.+AgentOps supports several types of spans, each representing a different kind of operation:-| Parameter | Type | Default | Example | Description |-| ----------- | ---- | ------- | ------------------------------ | --------------------------------------------------- |-| action_type | str | None | "Click" | Type of the action |-| logs | str | None | "Executed action successfully" | Logs generated during the action event |-| screenshot | str | None | "/path/to/screenshot.png" | Path to screenshot captured during the action event |+### Session Spans-An action or tool event can be used the [same way as other events](/v1/usage/recording-events) but also with the `record_action` or `record_tool`-decorators.+Session spans serve as the root for all other spans. They represent a complete execution of your agent workflow.-<CodeGroup>-```python python-from agentops import record_action+```python+from agentops.sdk.decorators import session-@record_action()-def some_action(params):- return "some response"+@session+def my_workflow():+ # Your session code here+ return result```-</CodeGroup>-This decorator will record an `ActionEvent` with the params and returns of the function as well as execution time.+### Agent Spans-## LLMEvent+Agent spans represent operations performed by a specific agent. They are typically children of session spans.-The LLMEvent class is for recording calls to LLMs. AgentOps auto-instruments calls to the most popular LLMs e.g. GPT, Claude, Gemini, etc.+```python+from agentops.sdk.decorators import agent-_In most cases, `LLMEvent` should not be used by the developer. These are created automatically._+@agent+class MyAgent:+ def __init__(self, name):+ self.name = name+```++### Operation/Task Spans-| Parameter | Type | Default | Example | Description |-| ----------------- | ------------- | ------- | ------------------------------------ | ------------------------------ |-| thread_id | UUID | None | 123e4567-e89b-12d3-a456-426614174000 | Thread ID of the event |-| prompt | str or List | None | "What's the weather today?" | The prompt for the event |-| prompt_tokens | int | None | 5 | Number of tokens in the prompt |-| completion | str or object | None | "It's sunny and warm." | The completion of the event |-| completion_tokens | int | None | 4 | Number of tokens in completion |-| model | Models or str | None | Models.MODEL_NAME or "model_name" | The model used for the event |+Operation spans represent specific tasks or operations performed by an agent. They are typically children of agent spans.-## ToolEvent+```python+from agentops.sdk.decorators import operation-The ToolEvent class is for recording calls to tools e.g. searchWeb, fetchFromDB.+@operation+def process_data(data):+ # Process the data+ return result+```-| Parameter | Type | Default | Example | Description |-| --------- | ----------- | ------- | ------------------------------------------------------- | ---------------------------- |-| name | str | None | "Parsing Tool" | Name of the tool |-| logs | str or dict | None | "Tool executed successfully" or `{"status": "success"}` | Logs from the tool execution |+### Workflow Spans-The tool event should be created before running the tool in order to track the execution time.+Workflow spans represent a sequence of operations that form a workflow. They can contain multiple operations.-<CodeGroup>-```python python-from agentops import ToolEvent, record-def scrape_website(url: str):- tool_event = ToolEvent(name='scrape_website', params={'url':url}) # the start timestamp is set when the obj is created- result = integration.scrape_website(data) # perform tool logic- tool_event.returns = result # set result- record(tool_event)+```python+from agentops.sdk.decorators import workflow++@workflow+def my_workflow(data):+ # Workflow implementation+ return result```-</CodeGroup>--## ErrorEvent--Error events can be used alone or in reference to another event. The can reference errors that the LLM made or errors-in execution, up to the developer.--| Parameter | Type | Default | Example | Description |-| ------------- | ----- | ------------------------------------------------ | --------------------------- | ----------------------------------------- |-| trigger_event | Event | None | Event instance | The Event that triggered the ErrorEvent |-| error_type | str | None | "Runtime Error" | Type of the error |-| code | str | None | "ERR001" | Code of the error |-| details | str | None | "Null pointer exception" | Detailed description of the error |-| logs | str | None | "Error occurred at line 10" | Logs related to the error |-| timestamp | str | A timestamp when the ErrorEvent was instantiated | "2023-03-15T18:59:13Z" | Timestamp when the ErrorEvent was created |--<CodeGroup>-```python python-from agentops import ToolEvent, record, ErrorEvent-def scrape_website(url: str):- tool_event = ToolEvent(name='scrape_website', params={'url':url}) # the start timestamp is set when the obj is created- try:- result = integration.scrape_website(data) # perform tool logic- tool_event.returns = result- except Error as e:- record(ErrorEvent(message=e, trigger_event=tool_event))- record(tool_event)++### LLM Spans++LLM spans are automatically created when you make calls to supported LLM providers. They represent interactions with language models.++## Span Attributes++All spans share a set of common attributes:++| Attribute | Description |+|-----------|-------------|+| span_id | A unique identifier for the span |+| trace_id | The ID of the trace this span belongs to |+| parent_id | The ID of the parent span |+| name | The name of the span |+| kind | The kind of span (session, agent, operation, etc.) |+| start_time | When the span started |+| end_time | When the span ended |+| attributes | Custom attributes for the span |++Additionally, spans automatically capture:++- Input parameters to the decorated function+- Return values from the decorated function+- Exceptions that occur during execution++## Span Hierarchy++Spans are organized hierarchically to create a trace of your agent's execution. The typical hierarchy is:++1. Session (root)+2. Agent+3. Operation/Task+4. Nested Operations++This hierarchy allows you to visualize the flow of your agent's execution and understand how different operations relate to each other.++## Error Handling++When an exception occurs within a decorated function, it's automatically recorded in the span. This allows you to easily identify and debug errors in your agent workflow.++```python+from agentops.sdk.decorators import operation++@operation+def risky_operation():+ # This exception will be recorded in the span+ raise ValueError("Something went wrong")```-</CodeGroup><script type="module" src="/scripts/github_stars.js"></script><script type="module" src="/scripts/adjust_api_dynamically.js"></script>\ No newline at end of filediff --git a/docs/v1/concepts/tags.mdx b/docs/v1/concepts/tags.mdxindex ac30a336..73243232 100644--- a/docs/v1/concepts/tags.mdx+++ b/docs/v1/concepts/tags.mdx@@ -8,38 +8,22 @@ Ex: `paid-user`, `v3`, `recruiter-agent`'# How to use Tags-There are two types: `tags` and `default tags`.--Tags belong to [Sessions](/v1/concepts/sessions) and are a fantastic way to organize your Sessions. When a session is created, `default_tags`-are automatically assigned to the session alongside any tags that are passed in.-+Tags belong to [Sessions](/v1/concepts/sessions) and are a fantastic way to organize your Sessions.## On `init()`-Passing `default_tags` into the `init` function will assign these tags to the all subsequent Sessions.--<CodeGroup>-```python python-agentops.init(default_tags=['test tag'])-```-</CodeGroup>--## `set_tags([str])`--This will override any existing tags on the session and set them to the value passed into the function.+Passing `tags` into the `init` function will assign these tags to the all subsequent Sessions.<CodeGroup>```python python-agentops.set_tags(['test new tag'])+agentops.init(tags=['test tag'])```</CodeGroup>-## `add_tags([str])`--This will append an array of tags to the session's existing tags.+`tags` can also be passed into the `start_session` function for better organization.<CodeGroup>```python python-agentops.add_tags(['appended tag'])+agentops.start_session(tags=['test tag'])```</CodeGroup>diff --git a/docs/v1/examples/camel.mdx b/docs/v1/examples/camel.mdxindex 7ea0459c..f70c2819 100644--- a/docs/v1/examples/camel.mdx+++ b/docs/v1/examples/camel.mdx@@ -58,7 +58,7 @@ Now we will initialize our AgentOps client.```python-agentops.init(default_tags=["camel", "multi-agent", "example"])+agentops.init(tags=["camel", "multi-agent", "example"])```Let's start with setting our task prompt and setting our tools.diff --git a/docs/v1/examples/langchain.mdx b/docs/v1/examples/langchain.mdxindex 00eb9162..bb073818 100644--- a/docs/v1/examples/langchain.mdx+++ b/docs/v1/examples/langchain.mdx@@ -65,7 +65,7 @@ Pass in your API key, and optionally any tags to describe this session for easie```pythonagentops_handler = AgentOpsLangchainCallbackHandler(- api_key=AGENTOPS_API_KEY, default_tags=["Langchain Example"]+ api_key=AGENTOPS_API_KEY, tags=["Langchain Example"])llm = ChatOpenAI(diff --git a/docs/v1/examples/multi_agent.mdx b/docs/v1/examples/multi_agent.mdxindex 840f0b79..6fbdc4dc 100644--- a/docs/v1/examples/multi_agent.mdx+++ b/docs/v1/examples/multi_agent.mdx@@ -1,6 +1,6 @@---title: 'Multi-Agent Example'-description: 'How to track events from multiple different agents'+description: 'How to track operations from multiple different agents'mode: "wide"---_View Notebook on <a href={'https://github.com/AgentOps-AI/agentops/blob/main/examples/multi_agent_example.ipynb'} target={'_blank'}>Github</a>_@@ -9,7 +9,7 @@ _View Notebook on <a href={'https://github.com/AgentOps-AI/agentops/blob/main/ex{/* SOURCE_FILE: examples/multi_agent_example.ipynb */}# Multi-Agent Support-This is an example implementation of tracking events from two separate agents+This is an example implementation of tracking operations from two separate agentsFirst let's install the required packages@@ -25,7 +25,7 @@ Then import them```pythonimport agentops-from agentops import track_agent+from agentops.sdk.decorators import agent, operationfrom openai import OpenAIimport osfrom dotenv import load_dotenv@@ -53,7 +53,7 @@ logging.basicConfig(```python-agentops.init(AGENTOPS_API_KEY, default_tags=["multi-agent-notebook"])+agentops.init(AGENTOPS_API_KEY, tags=["multi-agent-notebook"])openai_client = OpenAI(api_key=OPENAI_API_KEY)```@@ -61,8 +61,12 @@ Now lets create a few agents!```python-@track_agent(name="qa")+@agent(name="qa")class QaAgent:+ def __init__(self):+ pass++ @operationdef completion(self, prompt: str):res = openai_client.chat.completions.create(model="gpt-3.5-turbo",@@ -78,8 +82,12 @@ class QaAgent:return res.choices[0].message.content-@track_agent(name="engineer")+@agent(name="engineer")class EngineerAgent:+ def __init__(self):+ pass++ @operationdef completion(self, prompt: str):res = openai_client.chat.completions.create(model="gpt-3.5-turbo",@@ -101,7 +109,7 @@ qa = QaAgent()engineer = EngineerAgent()```-Now we have our agents and we tagged them with the `@track_agent` decorator. Any LLM calls that go through this class will now be tagged as agent calls in AgentOps.+Now we have our agents and we tagged them with the `@agent` decorator. Any LLM calls that go through this class will now be tagged as agent calls in AgentOps.Let's use these agents!diff --git a/docs/v1/examples/multion.mdx b/docs/v1/examples/multion.mdxindex 9200dc72..83c977b2 100644--- a/docs/v1/examples/multion.mdx+++ b/docs/v1/examples/multion.mdx@@ -92,7 +92,7 @@ When running `agentops.init()`, be sure to set `auto_start_session=False`. Multi```pythonagentops.init(- AGENTOPS_API_KEY, auto_start_session=False, default_tags=["MultiOn browse example"]+ AGENTOPS_API_KEY, auto_start_session=False, tags=["MultiOn browse example"])```diff --git a/docs/v1/examples/ollama.mdx b/docs/v1/examples/ollama.mdxindex 7f752375..1aeb58bc 100644--- a/docs/v1/examples/ollama.mdx+++ b/docs/v1/examples/ollama.mdx@@ -49,7 +49,7 @@ AGENTOPS_API_KEY = os.getenv("AGENTOPS_API_KEY") or "<your_agentops_key>"```python# Initialize AgentOps with some default tags-agentops.init(AGENTOPS_API_KEY, default_tags=["ollama-example"])+agentops.init(AGENTOPS_API_KEY, tags=["ollama-example"])```Now let's make some basic calls to Ollama. Make sure you have pulled the model first, use the following or replace with whichever model you want to use.diff --git a/docs/v1/examples/openai_assistants.mdx b/docs/v1/examples/openai_assistants.mdxindex e09857ae..bdf4aa71 100644--- a/docs/v1/examples/openai_assistants.mdx+++ b/docs/v1/examples/openai_assistants.mdx@@ -105,7 +105,7 @@ OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") or "<your_openai_key>"```python-agentops.init(api_key=AGENTOPS_API_KEY, default_tags=["openai", "beta-assistants"])+agentops.init(api_key=AGENTOPS_API_KEY, tags=["openai", "beta-assistants"])client = OpenAI(api_key=OPENAI_API_KEY)```diff --git a/docs/v1/examples/recording_events.mdx b/docs/v1/examples/recording_events.mdxindex 2302e525..5c28150f 100644--- a/docs/v1/examples/recording_events.mdx+++ b/docs/v1/examples/recording_events.mdx@@ -1,17 +1,17 @@---title: 'Recording Events'-description: 'How to record events in your session'+description: 'How to record events using spans in your session'mode: "wide"----_View Notebook on <a href={'https://github.com/AgentOps-AI/agentops/blob/main/examples/recording-events.ipynb'} target={'_blank'}>Github</a>_+_View Notebook on <a href={'https://github.com/AgentOps-AI/agentops/blob/main/examples/recording-operations.ipynb'} target={'_blank'}>Github</a>_-{/* SOURCE_FILE: examples/recording-events.ipynb */}+{/* SOURCE_FILE: examples/recording-operations.ipynb */}-# Recording Events-AgentOps has a number of different [Event Types](https://docs.agentops.ai/v1/details/events)+# Recording Operations with Spans+AgentOps v0.4 uses spans to track different types of operations in your agent workflows.-We automatically instrument your LLM Calls from OpenAI, LiteLLM, Cohere, and more. Just make sure their SDKs are imported before initializing AgentOps like we see below+We automatically instrument your LLM Calls from OpenAI, LiteLLM, Cohere, and more. Just make sure their SDKs are imported before initializing AgentOps like we see below.First let's install the required packages@@ -28,6 +28,7 @@ Then import them```pythonfrom openai import OpenAIimport agentops+from agentops.sdk.decorators import session, agent, operationimport osfrom dotenv import load_dotenv```@@ -49,90 +50,114 @@ AGENTOPS_API_KEY = os.getenv("AGENTOPS_API_KEY") or "<your_agentops_key>"```python-# Initialize the client, which will automatically start a session+# Initialize the clientagentops.init()# Optionally, we can add default tags to all sessions-# agentops.init(default_tags=['Hello Tracker'])+# agentops.init(tags=['Hello Tracker'])-openai = OpenAI()+openai_client = OpenAI()-messages = [{"role": "user", "content": "Hello"}]-response = openai.chat.completions.create(- model="gpt-3.5-turbo", messages=messages, temperature=0.5-)-print(response.choices[0].message.content)+# Create a session to track all operations+@session+def my_session():+ messages = [{"role": "user", "content": "Hello"}]+ response = openai_client.chat.completions.create(+ model="gpt-3.5-turbo", messages=messages, temperature=0.5+ )+ print(response.choices[0].message.content)++ # Call our operation+ result = add(2, 4)+ print(f"Addition result: {result}")++ # Call our agent+ agent = MyAgent()+ agent.process_data("test data")++# Run the session+my_session()```Click the AgentOps link above to see your session!-## Action Event+## Operations-AgentOps allows you to record other actions. The easiest way to record actions is through the use of AgentOps' decorators+AgentOps allows you to record operations using the `@operation` decorator:```python-from agentops import record_action-+from agentops.sdk.decorators import operation-@record_action("add numbers")+@operationdef add(x, y):return x + y---add(2, 4)```-We can also manually craft an event exactly the way we want by creating and recording an `ActionEvent`+## Agents+You can create agent spans that contain operations using the `@agent` decorator:-```python-from agentops import ActionEvent-agentops.record(- ActionEvent(- action_type="Agent says hello", params={"message": "Hi"}, returns="Hi Back!"- )-)+```python+from agentops.sdk.decorators import agent, operation++@agent+class MyAgent:+ @operation+ def process_data(self, data):+ print(f"Processing data: {data}")+ return f"Processed: {data.upper()}"```-## Tool Event-Agents use tools. These tools are useful to track with information such as name, end status, runtime, etc. To record tool usage, you can create and record a `ToolEvent` similar to above.+## Error Handling+Errors are automatically captured by the spans. When an exception occurs within a decorated function, it's recorded in the span:-```python-from agentops import ToolEvent, record+```python+from agentops.sdk.decorators import operation-def scrape_website(url: str):- tool_event = ToolEvent(- name="scrape_website", params={"url": url}- ) # the start timestamp is set when the obj is created- result = "scraped data" # perform tool logic- tool_event.returns = result- record(tool_event)-```+@operation+def risky_operation():+ # This exception will be recorded in the span+ try:+ 1 / 0 # Ooops! Something went wrong+ except Exception as e:+ print(f"Error occurred: {e}")+ raise-## Error Events-Error events can be used alone or in reference to another event. Lets add a catch block to the code above+# Create a session that includes the error+@session+def error_session():+ try:+ risky_operation()+ except Exception:+ print("Caught the error, but it's still recorded in the span")+# Run the error session+error_session()+```-```python-from agentops import ToolEvent, record, ErrorEvent+## Custom Span Attributes+You can add custom attributes to spans for additional context:-def scrape_website(url: str):- tool_event = ToolEvent(- name="scrape_website", params={"url": url}- ) # the start timestamp is set when the obj is created- try:- 1 / 0 # Ooops! Something went wrong- except Exception as e:- record(ErrorEvent(exception=e, trigger_event=tool_event))+```python+from agentops.sdk.decorators import operation+@operation(name="custom-operation")+def custom_operation(data):+ # Your operation logic here+ return f"Custom: {data}"-scrape_website("https://app.agentops.ai")+# Create a session with custom operation+@session(name="custom-session")+def custom_session():+ result = custom_operation("test")+ print(result)-agentops.end_session("Success")+# Run the custom session+custom_session()```diff --git a/docs/v1/examples/simple_agent.mdx b/docs/v1/examples/simple_agent.mdxindex 1727da13..93a8e888 100644--- a/docs/v1/examples/simple_agent.mdx+++ b/docs/v1/examples/simple_agent.mdx@@ -50,7 +50,7 @@ The AgentOps library is designed to be a plug-and-play replacement for the OpenA```pythonopenai = OpenAI(api_key=OPENAI_API_KEY)-agentops.init(AGENTOPS_API_KEY, default_tags=["openai-gpt-notebook"])+agentops.init(AGENTOPS_API_KEY, tags=["openai-gpt-notebook"])```Now just use OpenAI as you would normally!diff --git a/docs/v1/quickstart.mdx b/docs/v1/quickstart.mdxindex de62171d..4d3b8d64 100644--- a/docs/v1/quickstart.mdx+++ b/docs/v1/quickstart.mdx@@ -44,37 +44,53 @@ import EnvTooltip from '/snippets/add-env-tooltip.mdx'<CardGroup cols={1}>-<Card icon="code" title="Decorate Functions">- You can instrument other functions inside your code with the handy `@record_action`- decorator, which will record an `action_type`, the parameters, and the returns. You- will see these function calls alongside your LLM calls from instantiating the AgentOps client.+<Card icon="code" title="Decorate Operations">+ You can instrument functions inside your code with the `@operation` decorator, which will create spans that track function execution, parameters, and return values. These operations will be displayed in your session visualization alongside LLM calls.```python python- # (record specific functions)- @agentops.record_action('sample function being recorded')- def sample_function(...):- ...+ # Instrument a function as an operation+ from agentops.sdk.decorators import operation++ @operation+ def process_data(data):+ # Your function logic here+ result = data.upper()+ return result```</Card><Card icon="robot" title="Track Agents">- If you use specific named agents within your system, you can tie all downstream [Events](/v1/concepts/events) to a- Named Agent with the `@track_agent` decorator.+ If you use specific named agents within your system, you can create agent spans that contain all downstream operations using the `@agent` decorator.```python python- # (track a named agent)- @agentops.track_agent(name='my-expert-agent')- class sample_agent(...):- ...+ # Create an agent class+ from agentops.sdk.decorators import agent, operation++ @agent+ class MyAgent:+ def __init__(self, name):+ self.name = name++ @operation+ def perform_task(self, task):+ # Agent task logic here+ return f"Completed {task}"```</Card>-<Card icon="stop" title="Ending Your Session">- Finally, you should end your session by calling `.end_session()` indicating whether your session- was successful or not `(Success|Fail)`. We suggest setting session state depending on how- your agent exits or whether your agent succeeded or not. You can also specify a end state reason,- such as user interrupted, ran to completion, or unhandled exception.+<Card icon="stop" title="Creating Sessions">+ Create a session to group all your agent operations by using the `@session` decorator. Sessions serve as the root span for all operations.```python python- # End of program- agentops.end_session('Success')+ # Create a session+ from agentops.sdk.decorators import session++ @session+ def my_workflow():+ # Your session code here+ agent = MyAgent("research-agent")+ result = agent.perform_task("data analysis")+ return result++ # Run the session+ my_workflow()```</Card></CardGroup>@@ -85,24 +101,33 @@ import EnvTooltip from '/snippets/add-env-tooltip.mdx'Here is the complete code from the sections above```python python-import openaiimport agentops+from agentops.sdk.decorators import session, agent, operation-# Beginning of program (i.e. main.py, __init__.py)+# Initialize AgentOpsagentops.init(<INSERT YOUR API KEY HERE>)-# (record specific functions)[email protected]_action('sample function being record')-def sample_function(...):- ...--# (track a named agent)[email protected]_agent(name='my-expert-agent')-class sample_agent(...):- ...--# End of program-agentops.end_session('Success')+# Create an agent class+@agent+class MyAgent:+ def __init__(self, name):+ self.name = name++ @operation+ def perform_task(self, task):+ # Agent task logic here+ return f"Completed {task}"++# Create a session+@session+def my_workflow():+ # Your session code here+ agent = MyAgent("research-agent")+ result = agent.perform_task("data analysis")+ return result++# Run the session+my_workflow()```<Card@@ -114,7 +139,7 @@ agentops.end_session('Success')</Card><Check>- That's all you need to get started! Check out the documentation below to see how you can record other events. AgentOps is a lot more powerful this way!+ That's all you need to get started! Check out the documentation below to see how you can record other operations. AgentOps is a lot more powerful this way!</Check>## Explore our more advanced functionality!@@ -125,10 +150,10 @@ agentops.end_session('Success')icon="square-code"href="/v1/examples">- Record all of your other events the way AgentOps intends.+ Record all of your operations the way AgentOps intends.</Card><Card title="Tracking Agents" icon="robot" href="/v1/usage/tracking-agents">- Associate Events with specific named agents.+ Associate operations with specific named agents.</Card></CardGroup>diff --git a/docs/v1/usage/recording-events.mdx b/docs/v1/usage/recording-events.mdxdeleted file mode 100644index 99e365e2..00000000--- a/docs/v1/usage/recording-events.mdx+++ /dev/null@@ -1,57 +0,0 @@-----title: "Recording Events"-description: "Log events such as agent actions, LLM calls, tool calls, and errors. _See [Event Class](/v1/concepts/events)_."------To get the most out of AgentOps, it is best to carefully consider what events to record --not simply record whatever your agent is logging. AgentOps offers two ways to record events:--## `@record_action` Decorator--- **`event_type`** (str): Type of the event.--To make AgentOps easier to integrate, we also provide a function decorator to automatically create-and record an event for your function.--```python python-from agentops import record_action--@record_action('sample function being recorded')-def sample_function(...):- ...-```--The decorator will record the function's parameters, return values, and the time duration. We suggest using this on functions that take a long time and contain nested functions. For example, if you decorate a function that makes several OpenAI calls, then each OpenAI call will show in the replay graph as a child of the decorated function.--## `@record_tool` Decorator--- **`tool_name`** (str): The name of the tool represented by the python function--Additionally, we provide a function decorator to automatically create tool events for python functions.--```python python-from agentops import record_tool--@record_tool('SampleToolName')-def sample_tool(...):- ...-```--The decorator will record the function's parameters, returns, and the time duration. We suggest using this on functions that take a long time and contain nested functions. For example, if you decorate a function that makes several OpenAI calls, then each OpenAI call will show in the replay graph as a child of the decorated function.---## `record()` Method--From this point, simply call the `.record()` method in the AgentOps client:--Record any child of the [Event type](/v1/concepts/events) or ErrorEvent.--```python python-from agentops import record, ActionEvent-record(ActionEvent("event_type1"))-```--<script type="module" src="/scripts/github_stars.js"></script>-<script type="module" src="/scripts/scroll-img-fadein-animation.js"></script>-<script type="module" src="/scripts/button_heartbeat_animation.js"></script>-<script type="module" src="/scripts/adjust_api_dynamically.js"></script>diff --git a/docs/v1/usage/recording-operations.mdx b/docs/v1/usage/recording-operations.mdxnew file mode 100644index 00000000..ebf7f2f3--- /dev/null+++ b/docs/v1/usage/recording-operations.mdx@@ -0,0 +1,112 @@+---+title: "Recording Operations"+description: "Instrument your code with decorators to track operations, tasks, and workflows."+---++To get the most out of AgentOps, it's important to properly instrument your code to track operations. AgentOps provides a set of decorators that make this easy.++## Using Decorators for Instrumentation++AgentOps provides several decorators that allow you to easily instrument your code:++### `@operation` / `@task` Decorator++The `@operation` and `@task` decorators are aliases that create operation/task spans for tracking specific operations.++```python+from agentops.sdk.decorators import operation++@operation+def process_data(data):+ # Process the data+ return result+```++You can also use these decorators with parameters:++```python+@operation(name="custom-operation-name", version=1)+def process_data(data):+ # Process the data+ return result+```++When used within an agent class, operations are automatically nested under the agent:++```python+from agentops.sdk.decorators import agent, operation++@agent+class MyAgent:+ @operation+ def perform_task(self, task):+ # Operation implementation+ return result+```++### `@workflow` Decorator++The `@workflow` decorator creates workflow spans for tracking workflows, which can contain multiple operations.++```python+from agentops.sdk.decorators import workflow++@workflow+def my_workflow(data):+ # Workflow implementation+ return result+```++### `@agent` Decorator++The `@agent` decorator creates an agent span for tracking agent operations.++```python+from agentops.sdk.decorators import agent++@agent+class MyAgent:+ def __init__(self, name):+ self.name = name++ # Agent methods here+```++### `@session` Decorator++The `@session` decorator creates a session span, which serves as the root for all other spans.++```python+from agentops.sdk.decorators import session++@session+def my_workflow():+ # Your session code here+ return result+```++## Automatic Instrumentation++AgentOps automatically instruments calls to popular LLM providers (OpenAI, Anthropic, etc.). These calls are automatically tracked as LLM spans and are properly nested within your agent and operation spans.++## Best Practices++1. **Use the Right Decorator**: Choose the appropriate decorator based on what you're instrumenting:+ - `@session` for the root of your application+ - `@agent` for agent classes+ - `@operation` for specific operations+ - `@workflow` for sequences of operations++2. **Proper Nesting**: Ensure that your spans are properly nested:+ - Session spans should be at the top+ - Agent spans should be children of session spans+ - Operation spans should be children of agent spans++3. **Meaningful Names**: Use meaningful names for your spans to make them easier to identify in the dashboard.++4. **Record Important Data**: The decorators automatically record function parameters and return values, but you can also add custom attributes to spans for additional context.++<script type="module" src="/scripts/github_stars.js"></script>+<script type="module" src="/scripts/scroll-img-fadein-animation.js"></script>+<script type="module" src="/scripts/button_heartbeat_animation.js"></script>+<script type="module" src="/scripts/adjust_api_dynamically.js"></script>diff --git a/docs/v1/usage/tracking-agents.mdx b/docs/v1/usage/tracking-agents.mdxindex 12557201..6ee4f2a0 100644--- a/docs/v1/usage/tracking-agents.mdx+++ b/docs/v1/usage/tracking-agents.mdx@@ -1,56 +1,76 @@---title: "Tracking Agents"-description: "Use the `@track_agent()` decorator to tag your agents"+description: "Use the `@agent` decorator to create agent spans"----All Events are automatically associated with the agent that originated them. Agents are given a name which is what you will see in the dashboard.+All operations are automatically associated with the agent that originated them. Agents are given a name which is what you will see in the dashboard.-<Frame type="glass" caption="This event is labeled with the name of the Agent that originated it">+<Frame type="glass" caption="This operation is labeled with the name of the Agent that originated it"><img height="200" src="/images/agent-name.png" /></Frame>-The example below tags the agent MyAgent with name `SomeCustomName`.+The example below creates an agent class with a custom name:-```python python-from agentops import track_agent+```python+from agentops.sdk.decorators import agent-@track_agent(name='SomeCustomName')+@agent(name='ResearchAgent')class MyAgent:- ...+ def __init__(self):+ # Agent initialization+ pass++ # Agent methods```-Alternatively, to name an agent in runtime, the `@track_agent` decorator creates an additional keyword argument in the class constructor. Using the above example, you can create an agent with a dynamic name with:+If you don't specify a name, the agent will use the class name by default:+```python-research_agent = MyAgent(agentops_name='ResearchAgent')+@agent+class ResearchAgent:+ # This agent will have the name "ResearchAgent"+ pass```-`trackagent.name` is optional. <br></br>-If omitted, agent name defaults to the name of the class (e.g. MyAgent). <br></br>-If an event does not originate from a tracked agent, agent name defaults to "Default Agent".----## Assigning Agents to a Session-If you are using multiple concurrent sessions, it's important to assign a new agent to a session.+## Nesting Operations Under Agents-When the `@track_agent()` decorator is used on an agent class, it adds an additional keyword param `session`.+Operations performed by an agent should be decorated with the `@operation` decorator to ensure they're properly nested under the agent:```python-@track_agent()-class custom_agent:- def completion(self, ...):- ...--session = agentops.create_session()-agent = custom_agent(session=session)-agent.completion(...)--session.end_session()+from agentops.sdk.decorators import agent, operation++@agent+class ResearchAgent:+ @operation+ def search_web(self, query):+ # Search implementation+ return results++ @operation+ def analyze_data(self, data):+ # Analysis implementation+ return analysis```-In this example, we create a custom agent class. We initialize an agent object and pass in the session to ensure the agent is assigned properly.+## Session Context-<script type="module" src="/scripts/github_stars.js"></script>-<script type="module" src="/scripts/adjust_api_dynamically.js"></script>-<script type="module" src="/scripts/scroll-img-fadein-animation.js"></script>-<script type="module" src="/scripts/button_heartbeat_animation.js"></script>\ No newline at end of file+Agents should be created within a session context to ensure proper tracing:++```python+from agentops.sdk.decorators import session, agent, operation++@agent+class ResearchAgent:+ @operation+ def perform_research(self, topic):+ # Research implementation+ return results++@session+def research_workflow(topic):+ agent = ResearchAgent()+ return agent.perform_research(topic)++# Run the session+result = research_workflow("quantum computing")+```\ No newline at end of filediff --git a/examples/README.md b/examples/README.mdindex 6dca55bc..37f26370 100644--- a/examples/README.md+++ b/examples/README.md@@ -17,7 +17,7 @@ At a high level, AgentOps gives you the ability to monitor LLM calls, costs, lat## Quickstart- [Single LLM](./openai-gpt.ipynb)-- [Recording Events](./recording-events.ipynb)+- [Recording Events](./recording-operations.ipynb)- [Multi-Agent](./multi_agent_example.ipynb)## Integrationsdiff --git a/examples/recording-events.ipynb b/examples/recording-operations.ipynbsimilarity index 57%rename from examples/recording-events.ipynbrename to examples/recording-operations.ipynbindex f1c9e47c..9e2c9fc8 100644--- a/examples/recording-events.ipynb+++ b/examples/recording-operations.ipynb@@ -7,10 +7,10 @@"collapsed": false},"source": [- "# Recording Events\n",- "AgentOps has a number of different [Event Types](https://docs.agentops.ai/v1/details/events)\n",+ "# Recording Operations with Spans\n",+ "AgentOps v0.4 uses spans to track different types of operations in your agent workflows.\n","\n",- "We automatically instrument your LLM Calls from OpenAI, LiteLLM, Cohere, and more. Just make sure their SDKs are imported before initializing AgentOps like we see below"+ "We automatically instrument your LLM Calls from OpenAI, Anthropic, OpenAi Agents, and more. Just make sure their SDKs are imported before initializing AgentOps like we see below."]},{@@ -50,6 +50,7 @@"source": ["from openai import OpenAI\n","import agentops\n",+ "from agentops.sdk.decorators import session, agent, operation\n","import os\n","from dotenv import load_dotenv"]@@ -89,27 +90,33 @@},"outputs": [],"source": [- "# Initialize the client, which will automatically start a session\n",+ "# Initialize the client\n","agentops.init()\n","\n","# Optionally, we can add default tags to all sessions\n",- "# agentops.init(default_tags=['Hello Tracker'])\n",+ "# agentops.init(tags=['Hello Tracker'])\n","\n",- "openai = OpenAI()\n",+ "openai_client = OpenAI()\n","\n",- "messages = [{\"role\": \"user\", \"content\": \"Hello\"}]\n",- "response = openai.chat.completions.create(\n",- " model=\"gpt-3.5-turbo\", messages=messages, temperature=0.5\n",- ")\n",- "print(response.choices[0].message.content)"- ]- },- {- "cell_type": "markdown",- "id": "95a6047d",- "metadata": {},- "source": [- "Click the AgentOps link above to see your session!"+ "# Create a session to track all operations\n",+ "@session\n",+ "def my_session():\n",+ " messages = [{\"role\": \"user\", \"content\": \"Hello\"}]\n",+ " response = openai_client.chat.completions.create(\n",+ " model=\"gpt-3.5-turbo\", messages=messages, temperature=0.5\n",+ " )\n",+ " print(response.choices[0].message.content)\n",+ " \n",+ " # Call our operation\n",+ " result = add(2, 4)\n",+ " print(f\"Addition result: {result}\")\n",+ " \n",+ " # Call our agent\n",+ " agent = MyAgent()\n",+ " agent.process_data(\"test data\")\n",+ " \n",+ "# Run the session\n",+ "my_session()"]},{@@ -119,9 +126,9 @@"collapsed": false},"source": [- "## Action Event\n",- "\n",- "AgentOps allows you to record other actions. The easiest way to record actions is through the use of AgentOps' decorators"+ "Click the AgentOps link above to see your session!\n",+ "## Operations\n",+ "AgentOps allows you to record operations using the `@operation` decorator:"]},{@@ -133,15 +140,11 @@},"outputs": [],"source": [- "from agentops import record_action\n",- "\n",+ "from agentops.sdk.decorators import operation\n","\n",- "@record_action(\"add numbers\")\n",+ "@operation\n","def add(x, y):\n",- " return x + y\n",- "\n",- "\n",- "add(2, 4)"+ " return x + y"]},{@@ -151,7 +154,8 @@"collapsed": false},"source": [- "We can also manually craft an event exactly the way we want by creating and recording an `ActionEvent`"+ "## Agents\n",+ "You can create agent spans that contain operations using the `@agent` decorator:"]},{@@ -163,13 +167,14 @@},"outputs": [],"source": [- "from agentops import ActionEvent\n",+ "from agentops.sdk.decorators import agent, operation\n","\n",- "agentops.record(\n",- " ActionEvent(\n",- " action_type=\"Agent says hello\", params={\"message\": \"Hi\"}, returns=\"Hi Back!\"\n",- " )\n",- ")"+ "@agent\n",+ "class MyAgent:\n",+ " @operation\n",+ " def process_data(self, data):\n",+ " print(f\"Processing data: {data}\")\n",+ " return f\"Processed: {data.upper()}\""]},{@@ -179,8 +184,8 @@"collapsed": false},"source": [- "## Tool Event\n",- "Agents use tools. These tools are useful to track with information such as name, end status, runtime, etc. To record tool usage, you can create and record a `ToolEvent` similar to above."+ "## Error Handling\n",+ "Errors are automatically captured by the spans. When an exception occurs within a decorated function, it's recorded in the span:"]},{@@ -192,16 +197,27 @@},"outputs": [],"source": [- "from agentops import ToolEvent, record\n",+ "from agentops.sdk.decorators import operation\n","\n",+ "@operation\n",+ "def risky_operation():\n",+ " # This exception will be recorded in the span\n",+ " try:\n",+ " 1 / 0 # Ooops! Something went wrong\n",+ " except Exception as e:\n",+ " print(f\"Error occurred: {e}\")\n",+ " raise\n","\n",- "def scrape_website(url: str):\n",- " tool_event = ToolEvent(\n",- " name=\"scrape_website\", params={\"url\": url}\n",- " ) # the start timestamp is set when the obj is created\n",- " result = \"scraped data\" # perform tool logic\n",- " tool_event.returns = result\n",- " record(tool_event)"+ "# Create a session that includes the error\n",+ "@session\n",+ "def error_session():\n",+ " try:\n",+ " risky_operation()\n",+ " except Exception:\n",+ " print(\"Caught the error, but it's still recorded in the span\")\n",+ "\n",+ "# Run the error session\n",+ "error_session()"]},{@@ -211,8 +227,8 @@"collapsed": false},"source": [- "## Error Events\n",- "Error events can be used alone or in reference to another event. Lets add a catch block to the code above"+ "## Custom Span Attributes\n",+ "You can add custom attributes to spans for additional context:"]},{@@ -224,23 +240,21 @@},"outputs": [],"source": [- "from agentops import ToolEvent, record, ErrorEvent\n",- "\n",- "\n",- "def scrape_website(url: str):\n",- " tool_event = ToolEvent(\n",- " name=\"scrape_website\", params={\"url\": url}\n",- " ) # the start timestamp is set when the obj is created\n",- "\n",- " try:\n",- " 1 / 0 # Ooops! Something went wrong\n",- " except Exception as e:\n",- " record(ErrorEvent(exception=e, trigger_event=tool_event))\n",+ "from agentops.sdk.decorators import operation\n","\n",+ "@operation(name=\"custom-operation\")\n",+ "def custom_operation(data):\n",+ " # Your operation logic here\n",+ " return f\"Custom: {data}\"\n","\n",- "scrape_website(\"https://app.agentops.ai\")\n",+ "# Create a session with custom operation\n",+ "@session(name=\"custom-session\")\n",+ "def custom_session():\n",+ " result = custom_operation(\"test\")\n",+ " print(result)\n","\n",- "agentops.end_session(\"Success\")"+ "# Run the custom session\n",+ "custom_session()"]}],