Agent
Agent is an entity that is responsible for doing one job (for example being a shopping assistant). Here’s the simplest possible definition of an agent:simple_agent. It has a single run type which input and output item schemas are defined.
The Agent definition above is enough to create a session and add a bunch of items to it.
Data Model
Here’s a brief overview of the data model:- Agent is a configuration object, think of it as a class.
- Session represents a multi-turn conversation with an agent, it’s an instance of Agent.
- Run is a single turn of the conversation. It is composed of a list of arbitrary JSON objects called Items.
- The first Item in a Run is considered to be the “input” item. It’s usually the user message.
- The last Item in a successful Run is considered to be the “output” item. It’s usually the assistant message.
- Items between input and output are called “steps”. They can be tool calls, server tools, reasoning blocks, hand-offs to other agents, etc.
- Session might have multiple Run types, each with different input, output and step item schemas.
Session
Session represents a multi-turn conversation with the agent.Create a session
Read session
Session Items
The core part of each session state is an ordered list of Session Items, or just Items. Items can be added via Runs (we explain Runs later in this chapter).- user or assistant messages
- reasoning blocks
- tools (function calls, MCP, server tools)
- handoffs to other agents in multi-agent systems
- any custom block
items they’ll be appended at the end of the list. Removing items is not possible.
Each Item is displayed as a separate block in Studio, can have its own custom component, can be commentable, can have its custom scores, feedback etc.
It’s worth highlighting that Item in AgentView is just a JSON, it can be anything. AgentView goal is to be as unopinionated as possible and therefore doesn’t force you into any pre-defined message formats. This allows to make it really framework-agnostic. For example, it’s trivial to just send items from AgentsSDK, Responses API, AI SDK, LangChain, without any trouble.
Session state
Sometimes it’s hard to represent a session state just with array of Items. For example, durign the course of the session you might create session memories, or additional artifacts needed for thread summarisation (to prevent context overflow). You might want to store them between runs, but, it’s unnatural to keep them as Items.
For that purpose AgentView introduced the concept of the state. State is totally arbitrary object bounded to a session available via session.state.
Session Metadata
Sessions can have custom metadata. AgentView aims to be as flexible as possible, that’s why every object can be extended custom metadata. Imagine you’re building a shopping assistant which can run on the product page of e-commerce website. In that case each session must haveproduct_id assigned.
Let’s update our agent config:
metadata is an object with so called “metafields”. Each metafield has a key (name) and it’s Zod schema.
Now when we create a new session we must provide product_id (since it’s not .optional()):
createSession without providing product_id the createSession function would throw an error.
By default, if metafield is not defined in metadata, it’s allowed to add it (no validation). If you want to override it:
Updating metafields
You can change the value of metafield viaav.updateSession:
Clearing metafields
You can clear metafield by setting it tonull:
Runs
You add items to a session via runs. Run is a special object helping in managing lifecycle of the session. Run represents a single turn of the conversation. It starts with a user action, usually just a user message, an input item. Then the any number of items might be produced, and in the end the output item must be added, which ends the run. Run in more complex systems will usually consist of multiple calls to the LLMs, tool calls, etc. Here’s how to create a run with a user message input message.Run status
The runstatus can have one of the following values: in_progress, complete, failed or cancelled.
After the run is created it automatically has in_progress value. When the run is in_progress, the session won’t accept any new runs. Below, the second av.createRun will result in an exception:
Adding items
You can callav.updateRun as many times as you want to keep adding items.
Ending Run
If the run finished successfully, just set the status tocomplete:
cancelled status is used to indicate this case:
complete, failed or cancelled), you can’t change its status nor add items anymore.
Retry runs
If the last session run wasfailed or cancelled, you can still create a new run. If you do it, the last run (the one that was failed or cancelled) will be removed from the history and the new run will be appended to the last successful run:
Retry logic with keeping only the input message from the failed run:
Resume runs
Removing items
You can’t remove items from the session. The core foundation behind AgentView is that whenever you send an item to a client (or agentview), the end user saw it. If end user saw it, then anyone using AgentView Studio should also be able to see it, exactly as user did.Run metadata
Similary to sessions, runs can be extended with custom metadata. It’s helpful when you want to store usage data, trace id for observability platform link, etc.allowUnknownMetadata:
Long running jobs
By default, the run will automatically be set as failed after 60 seconds of inactivity. Each time you callav.updateRun, the timer is reset.
You can reset timer manually by calling av.keepAliveRun:
idleTimeout in the agent config: