Evaluation Sessions
Learn how to group evaluations into sessions for context and audit.
Overview
Evaluation sessions group related evaluations into a single logical workflow. Instead of treating each evaluation as an isolated event, sessions let the policy engine see the full history of what has happened so far -- which tools were called, what data was accessed, how many actions have occurred -- and make decisions based on that context.
When to Use Sessions
Sessions are useful whenever you have multi-step workflows where individual actions should be evaluated in context:
For simple, one-off evaluations (a single API request that does not relate to other requests), sessions are optional. If you are using an agent integration, sessions are auto-created for you -- you do not need to manage them manually unless you want to control the lifecycle.
Agent integrations automatically create a session if no sessionId is provided in the evaluation request. The auto-created session ID is returned in the response.
Session Lifecycle
Sessions follow a simple state machine:
ACTIVE ──> COMPLETED (ended normally)
|
├──────> EXPIRED (past expiresAt timestamp)
|
└──────> TERMINATED (ended early due to violation or manual stop)
expiresAt timestamp has passed. No further evaluations can be added.Using the SDK
The simplest way to use sessions is with the official SDK. Create a session, pass the sessionId to each evaluation, and end the session when the workflow is complete.
import Aguardic from "@aguardic/sdk";
const aguardic = new Aguardic(process.env.AGUARDIC_API_KEY);
// 1. Create a session
const session = await aguardic.sessions.create({
externalSessionId: "agent-conv-2025-03-10-abc",
metadata: { userId: "user-456", channel: "web-chat" },
});
// 2. Evaluate actions within the session
const result1 = await aguardic.evaluate({
sessionId: session.id,
input: { tool: "query_database", arguments: { query: "SELECT name, email FROM customers WHERE id = 123" } },
targetKey: "tool-call",
});
console.log("Action 1:", result1.outcome);
const result2 = await aguardic.evaluate({
sessionId: session.id,
input: { tool: "send_email", arguments: { to: "external@example.com", body: "Customer details: John Doe" } },
targetKey: "tool-call",
});
console.log("Action 2:", result2.outcome);
// 3. End the session
const summary = await aguardic.sessions.end(session.id, { status: "COMPLETED" });
console.log("Actions:", summary.actionCount, "Tools:", summary.toolsUsed);Don't have the SDK? See the JavaScript / TypeScript SDK for installation and setup. The rest of this guide shows the equivalent raw fetch calls for reference.
Raw API Example
Here is the same workflow using raw fetch calls if you are not using the SDK.
1. Create the session
const API_BASE = "https://api.aguardic.com/v1";
const API_KEY = process.env.AGUARDIC_API_KEY;
const headers = {
Authorization: `Bearer ${API_KEY}`,
"Content-Type": "application/json",
};
const sessionRes = await fetch(`${API_BASE}/evaluation-sessions`, {
method: "POST",
headers,
body: JSON.stringify({
externalSessionId: "agent-conv-2025-03-10-abc",
metadata: { userId: "user-456", channel: "web-chat" },
}),
});
const session = await sessionRes.json();
const sessionId = session.data.id;2. Evaluate actions within the session
Pass the sessionId to each evaluation request. Aguardic automatically tracks each evaluation as an action in the session.
const eval1 = await fetch(`${API_BASE}/evaluate`, {
method: "POST",
headers,
body: JSON.stringify({
sessionId,
input: {
tool: "query_database",
arguments: { query: "SELECT name, email FROM customers WHERE id = 123" },
},
targetKey: "tool-call",
}),
});
const result1 = await eval1.json();
console.log("Action 1 outcome:", result1.data.outcome);
const eval2 = await fetch(`${API_BASE}/evaluate`, {
method: "POST",
headers,
body: JSON.stringify({
sessionId,
input: {
tool: "send_email",
arguments: {
to: "external@example.com",
subject: "Customer Details",
body: "Here are the customer details: John Doe, john@example.com",
},
},
targetKey: "tool-call",
}),
});
const result2 = await eval2.json();
console.log("Action 2 outcome:", result2.data.outcome);3. End the session
const endRes = await fetch(
`${API_BASE}/evaluation-sessions/${sessionId}/end`,
{
method: "POST",
headers,
body: JSON.stringify({ status: "COMPLETED" }),
}
);
const ended = await endRes.json();
console.log("Session ended:", ended.data.status);
console.log("Final action count:", ended.data.actionCount);Context-Aware Policy Evaluation
The real power of sessions is context-aware policy evaluation. When you include a sessionId in an evaluation request, the policy engine automatically receives the session's accumulated context:
| Field | Type | Description |
|---|---|---|
actionCount | number | Total number of evaluations run so far in the session. |
dataTags | string[] | Aggregated data categories detected across all previous actions (e.g., ["pii", "financial"]). |
toolsUsed | string[] | All tools or operations invoked so far in the session. |
recentActions | array | The most recent actions in the session for pattern detection. |
This context is available to both deterministic and semantic rules. Here are examples of policies you can build with session context:
actionCount exceeds 20 in a single session.dataTags will contain "pii" from a previous action).Without sessions, each evaluation is independent -- the policy engine has no way to know that the agent queried PII data two steps ago before trying to email it externally.
Best Practices
expiresAt, explicitly ending them with COMPLETED or TERMINATED provides a cleaner audit trail.Next Steps
- Sessions API Reference -- Full API documentation for session endpoints
- Evaluate API -- How to send evaluations with session context
- Audit Trail -- Investigate violations across sessions
- AI Systems -- Register AI systems and link integrations