Data Models

Pydantic models that power AgentLens event tracking.

All data models use Pydantic v2 for validation and serialization. Import them from agentlens.models.

AgentEvent

The core model — a single observable event in an agent's execution.

from agentlens.models import AgentEvent

event = AgentEvent(
    event_type="llm_call",
    model="gpt-4",
    input_data={"prompt": "Hello"},
    output_data={"response": "Hi there"},
    tokens_in=5,
    tokens_out=3,
    duration_ms=150.0,
)
FieldTypeDefaultDescription
event_idstrauto (UUID hex)Unique event identifier
session_idstr""Parent session ID
event_typestr"generic"Category: llm_call, tool_call, agent_call, agent_error, tool_error, decision, generic
timestampdatetimenow (UTC)When the event occurred
input_datadict | NoneNoneInput to the operation
output_datadict | NoneNoneOutput from the operation
modelstr | NoneNoneLLM model name
tokens_inint0Input token count
tokens_outint0Output token count
tool_callToolCall | NoneNoneAssociated tool call
decision_traceDecisionTrace | NoneNoneAssociated reasoning
duration_msfloat | NoneNoneExecution time in milliseconds

Methods

to_api_dict()

Converts the event to a dictionary suitable for the REST API (JSON-serializable, excludes None fields).

event = AgentEvent(event_type="llm_call", model="gpt-4", tokens_in=100)
api_dict = event.to_api_dict()
# {"event_id": "a1b2c3...", "session_id": "", "event_type": "llm_call",
#  "model": "gpt-4", "tokens_in": 100, "tokens_out": 0, ...}

ToolCall

Represents a single tool/function call made by an agent.

from agentlens.models import ToolCall

tc = ToolCall(
    tool_name="web_search",
    tool_input={"query": "AI safety papers"},
    tool_output={"results": [...]},
    duration_ms=890.2,
)
FieldTypeDefaultDescription
tool_call_idstrauto (UUID hex)Unique identifier
tool_namestrrequiredName of the tool
tool_inputdict{}Input parameters
tool_outputdict | NoneNoneReturn value
timestampdatetimenow (UTC)When the call was made
duration_msfloat | NoneNoneExecution time in ms

DecisionTrace

Captures the reasoning behind an agent decision.

from agentlens.models import DecisionTrace

dt = DecisionTrace(
    reasoning="The user asked about weather, so I need to call the weather API rather than using cached data.",
    step=3,
    alternatives_considered=["Use cached data", "Ask user for location first"],
    confidence=0.92,
)
FieldTypeDefaultDescription
trace_idstrauto (UUID hex)Unique identifier
stepint0Step number in the execution sequence
reasoningstr""Why this decision was made
alternatives_consideredlist[str][]Other options the agent considered
confidencefloat | NoneNoneConfidence score (0.0–1.0)
timestampdatetimenow (UTC)When the decision was made

Session

Represents an agent tracking session — a collection of events for one agent run.

from agentlens.models import Session

session = Session(
    agent_name="research-agent",
    metadata={"version": "2.0", "env": "prod"},
)
FieldTypeDefaultDescription
session_idstrauto (UUID hex)Unique session identifier
agent_namestr"default-agent"Name of the agent
started_atdatetimenow (UTC)Session start time
ended_atdatetime | NoneNoneSession end time
metadatadict{}Arbitrary metadata
eventslist[AgentEvent][]Events in this session
total_tokens_inint0Cumulative input tokens
total_tokens_outint0Cumulative output tokens
statusstr"active"active, completed, or error

Methods

add_event(event)

Add an event to the session. Automatically sets the event's session_id and updates token counters.

end()

Mark the session as completed and set ended_at to now.

to_api_dict()

Convert to a dictionary for the API (excludes the events list for lightweight session listing).