What is an Agent Session?
An Agent Session represents a continuous thread of interaction between a user (or an automated system) and an AI Agent. It persists:- Message History: The full back-and-forth conversation, including branching paths.
- Context: Documents and files linked specifically to this conversation.
- Tool State: Which tools are active and available for the agent to use.
- User Isolation: Security boundaries to ensure one user’s session is private from others.
Creating a Session
To start a chat, you first create a session. You can initialize it with a specific Assistant (which comes with pre-defined tools and instructions) or a raw System Prompt.Multi-Tenancy & User Isolation
If you are building an application for your own users, you should use theexternal_user_id field. This feature is designed to simplify multi-tenant integrations by allowing you to manage millions of end-users with a single UBIK API Key.
The API enforces a Hybrid Access Model for documents and a Strict Isolation Model for sessions and tools.
For a detailed explanation of how data isolation works and how to implement it (Server-Side vs. Client-Side), please refer to our Authentication & Security Guide.
Sending Messages
Once a session is created, you can send messages to it. The agent will process your message, potentially use tools, and generate a response.cURL
Streaming Responses
For a real-time chat experience, set"stream": true. The API will return a Server-Sent Events (SSE) stream directly in the response.
For a complete reference of the events you can receive, see the Agent Session Events Guide.
Dedicated Stream Endpoint & Reconnection
While thePOST endpoint allows direct streaming, you may sometimes need a dedicated stream endpoint (e.g., for reconnecting to a dropped stream or consuming events from a separate client).
Endpoint: GET /agent-sessions/{session_id}/stream
This endpoint streams events for the current or most recent agent task. It automatically replays past events for that task before switching to live mode, ensuring you don’t miss any context upon reconnection.
Authentication with EventSource (JWT)
Standard browser EventSource APIs do not support custom headers (like X-API-KEY or Authorization). To securely connect from a browser, you can pass a short-lived JWT token via the token query parameter.
- Generate a Token: Use
POST /auth/token(see Authentication & Security Guide). - Connect:
Managing Context
You can dynamically add knowledge to a session. This is powerful for “Chat with your Data” use cases.Linking Existing Documents
If you’ve already uploaded documents to UBIK, you can link them to the session:cURL
Uploading Directly to Session
You can also upload a file and link it in one step:cURL
Advanced Features
Branching & Navigation
UBIK sessions support branching. If a user edits a message or regenerates a response, it creates a new “branch” in the conversation tree without losing the original history.- Regenerate:
POST /agent-sessions/{id}/messages/{msg_id}/regenerate - Edit:
POST /agent-sessions/{id}/messages/{msg_id}/edit - Navigate:
POST /agent-sessions/{id}/navigate(Switch active branch)
Checkpoints & Resuming
Long-running tasks or complex conversations automatically save Checkpoints at key steps. You can list these and restore the session state to a previous point. This is particularly useful for multi-step workflows where the agent might make a mistake in the final step.- List Checkpoints:
GET /agent-sessions/{id}/checkpoints - Restore:
POST /agent-sessions/{id}/navigate-to-checkpoint
- Summarize “Annual Report 2023.pdf” (Success)
- Summarize “Market Trends 2024.pdf” (Success)
- Write a newsletter combining both summaries. (Failure: Agent missed crucial financial data)
- List Checkpoints: Find the checkpoint ID corresponding to the state after step 2.
- Restore & Guide: Restore that checkpoint and provide a
checkpoint_hintto correct the agent’s course.
cURL
checkpoint_hint parameter is powerful: it injects a specific instruction right at the restoration point, guiding the agent’s next action without altering the previous successful steps.
