---
type: api
title: Save session events
description: "Saves the events of an agent session, one document per event, so the session can be searched later. User and assistant messages are indexed for semantic search."
url: https://www.elastic.co/elastic/docs-builder/docs/5002/api/kibana/next/operation/operation-post-context-engine-memory-save-event
resource: https://www.elastic.co/elastic/docs-builder/docs/5002/api/kibana/next/operation/operation-post-context-engine-memory-save-event
products:
  - Kibana
---

# Save session events

Authentication: required

Availability: Planned

- Server: `https://{kibana_url}`

## Paths

- `POST` `/api/context_engine/memory/_save_event`
- `POST` `/s/{space_id}/api/context_engine/memory/_save_event`

#### Path parameters

- `space_id` (string) — optional
  An identifier for the space. If `space_id` is not provided in the URL, the default space is used. Refer to [Spaces](https://www.elastic.co/docs/deploy-manage/manage-spaces) for more information.

## Description

Saves the events of an agent session, one document per event, so the session can be searched later. User and assistant messages are indexed for semantic search.

Takes 1 to 1000 events and a body of up to 10 MB. A request that fails validation is rejected as a whole. Any field an event carries beyond the documented ones, such as a tool event's `result` or a tool call's `args`, is stored as sent, whatever its JSON type.

Each event gets its own result, in request order. A `failed` event, for example when its content could not be embedded, does not fail the request; resend it to retry. A resent event is stored again, and readers drop duplicates by session and event id.

The route needs the `contextEngine:write` privilege. Without `user_id` the events belong to the caller's user profile, and a caller without one gets a 403. Naming another user in `user_id` also needs `contextEngine:manage_memory`. Each AI index in `ai_index_ids` must exist in this space, and the caller needs the Elasticsearch `write` privilege on its backing index. Otherwise the request is rejected and nothing is saved.

After authorization, validation, and feature-flag checks pass, each request writes one `memory_save_event` audit event.

Returns a 404 response when Context Engine is turned off in this space (`contextEngine:enabled`), when memory is turned off (`contextEngine:memoryEnabled`), or when an AI index in `ai_index_ids` does not exist.

**For more information, refer to the [Context Engine documentation](https://www.elastic.co/docs/explore-analyze/ai-features/context-engine).**

## Required privileges

- Privileges: `contextEngine:write. Extended privileges (optional, expand functionality when granted): contextEngine:manage_memory`

## Header parameters

- `kbn-xsrf` (string) — required
  A required header to protect against CSRF attacks

## Request body

`application/json`

- `ai_index_ids` (string[] · max items: 100) — optional
  The AI indices the agent could use in the session. Each must exist in this space, and the caller needs the Elasticsearch `write` privilege on its backing index.
- `events` (union oneOf[] · min items: 1 · max items: 1000) — required
  The events to save.
  - **context_engine_session_event_user**
    A user message event.
    - `type` (enum) — required
      Values: `user`
    - `content` (string · max length: 1048576) — required
      The user message. It is indexed for semantic search.
    - `event_at` (string · max length: 64) — required
      When the agent created the event, as an ISO 8601 date and time such as `2026-11-16T09:58:00.000Z`.
    - `id` (string · min length: 1 · max length: 256) — required
      The event id, unique within the session. Readers drop resent duplicates by session and event id.
    - `seq` (number · min: 0 · max: 9007199254740991) — optional
      An integer order key for events within the session.
    - `turn_id` (string · min length: 1 · max length: 256) — required
      The turn the event belongs to. Events are grouped into turns by this id.
  - **context_engine_session_event_assistant**
    An assistant message or tool call request event.
    - `type` (enum) — required
      Values: `assistant`
    - `content` (string · max length: 1048576) — optional
      The assistant message. It is indexed for semantic search.
    - `event_at` (string · max length: 64) — required
      When the agent created the event, as an ISO 8601 date and time such as `2026-11-16T09:58:00.000Z`.
    - `id` (string · min length: 1 · max length: 256) — required
      The event id, unique within the session. Readers drop resent duplicates by session and event id.
    - `seq` (number · min: 0 · max: 9007199254740991) — optional
      An integer order key for events within the session.
    - `tool_calls` (object[] · max items: 128) — optional
      The tool calls the assistant made in this message. Any other field of a call, such as `args` as a string or an object, is stored as sent.
      - `id` (string · min length: 1 · max length: 256) — required
        The tool call id that a later tool event refers to in `tool_call_id`.
      - `name` (string · min length: 1 · max length: 256) — required
        The name of the called tool.
    - `turn_id` (string · min length: 1 · max length: 256) — required
      The turn the event belongs to. Events are grouped into turns by this id.
  - **context_engine_session_event_reasoning**
    A model reasoning text event.
    - `type` (enum) — required
      Values: `reasoning`
    - `content` (string · max length: 1048576) — optional
      The reasoning text. It is stored but not indexed.
    - `event_at` (string · max length: 64) — required
      When the agent created the event, as an ISO 8601 date and time such as `2026-11-16T09:58:00.000Z`.
    - `id` (string · min length: 1 · max length: 256) — required
      The event id, unique within the session. Readers drop resent duplicates by session and event id.
    - `seq` (number · min: 0 · max: 9007199254740991) — optional
      An integer order key for events within the session.
    - `turn_id` (string · min length: 1 · max length: 256) — required
      The turn the event belongs to. Events are grouped into turns by this id.
  - **context_engine_session_event_tool**
    A tool execution result event.
    - `type` (enum) — required
      Values: `tool`
    - `event_at` (string · max length: 64) — required
      When the agent created the event, as an ISO 8601 date and time such as `2026-11-16T09:58:00.000Z`.
    - `id` (string · min length: 1 · max length: 256) — required
      The event id, unique within the session. Readers drop resent duplicates by session and event id.
    - `seq` (number · min: 0 · max: 9007199254740991) — optional
      An integer order key for events within the session.
    - `status` (enum) — optional — nullable
      Whether the tool succeeded. Leave it out or send null when unknown; it is stored as `unknown`.
      Values: `successful`, `failed`, `null`
    - `tool_call_id` (string · min length: 1 · max length: 256) — required
      The id of the tool call this event answers.
    - `tool_name` (string · min length: 1 · max length: 256) — required
      The name of the tool that ran.
    - `turn_id` (string · min length: 1 · max length: 256) — required
      The turn the event belongs to. Events are grouped into turns by this id.
  - **context_engine_session_event_meta**
    A session metadata event carrying custom fields.
    - `type` (enum) — required
      Values: `meta`
    - `event_at` (string · max length: 64) — required
      When the agent created the event, as an ISO 8601 date and time such as `2026-11-16T09:58:00.000Z`.
    - `id` (string · min length: 1 · max length: 256) — required
      The event id, unique within the session. Readers drop resent duplicates by session and event id.
    - `seq` (number · min: 0 · max: 9007199254740991) — optional
      An integer order key for events within the session.
    - `turn_id` (string · min length: 1 · max length: 256) — required
      The turn the event belongs to. Events are grouped into turns by this id.
- `session_id` (string · min length: 1 · max length: 256) — required
  The session the events belong to.
- `user_id` (string · min length: 1 · max length: 1024) — optional
  The Kibana user profile UID that the events belong to. Defaults to the caller's user profile. Naming another user requires `contextEngine:manage_memory`.

## Responses

#### `200`

The result for every event.

`application/json`

- `results` (object[] · max items: 1000)
  One result per event, in request order.
  - `error` (string)
    Why the event failed. Resend it to retry.
  - `id` (string)
    The event id.
  - `status` (enum)
    Whether the event was stored.
    Values: `created`, `failed`
#### `403`

`user_id` is missing and the caller has no user profile, `user_id` names another user and the caller lacks `contextEngine:manage_memory`, or the caller cannot write to one of the AI indices.

`application/json`

- `error` (string)
  The HTTP status text.
- `message` (string)
  A human-readable error message.
- `statusCode` (number)
  The HTTP status code.
#### `404`

Context Engine or memory is turned off, or an AI index does not exist.

`application/json`

- `error` (string)
  The HTTP status text.
- `message` (string)
  A human-readable error message.
- `statusCode` (number)
  The HTTP status code.
## Code examples

#### Console

```console
POST kbn:/api/context_engine/memory/_save_event
{
  "session_id": "3f9c2e1a-7b4d-4c8e-9a61-5d2f0b7e8c43",
  "ai_index_ids": ["ops-memory"],
  "events": [
    {
      "id": "evt-001",
      "type": "user",
      "turn_id": "turn-1",
      "event_at": "2026-11-16T09:58:00.000Z",
      "seq": 1,
      "content": "Why is checkout latency up since this morning?"
    },
    {
      "id": "evt-002",
      "type": "assistant",
      "turn_id": "turn-1",
      "event_at": "2026-11-16T09:58:02.000Z",
      "seq": 2,
      "tool_calls": [
        { "id": "call-1", "name": "search_logs", "args": { "index": "logs-checkout*", "since": "now-6h" } }
      ]
    },
    {
      "id": "evt-003",
      "type": "tool",
      "turn_id": "turn-1",
      "event_at": "2026-11-16T09:58:32.000Z",
      "seq": 3,
      "tool_call_id": "call-1",
      "tool_name": "search_logs",
      "status": "failed",
      "result": "Search timed out after 30 seconds",
      "duration_ms": 30000
    },
    {
      "id": "evt-004",
      "type": "assistant",
      "turn_id": "turn-1",
      "event_at": "2026-11-16T09:58:35.000Z",
      "seq": 4,
      "content": "The log search timed out, so I could not confirm it from the logs. The checkout-service deploy at 08:40 is the most likely cause. Check its connection pool settings, or roll it back."
    }
  ]
}
```

#### curl

```curl
curl \
  -X POST "https://${KIBANA_URL}/api/context_engine/memory/_save_event" \
  -H "Authorization: ApiKey ${API_KEY}" \
  -H "Content-Type: application/json" \
  -H "kbn-xsrf: true" \
  -d '{
    "session_id": "3f9c2e1a-7b4d-4c8e-9a61-5d2f0b7e8c43",
    "ai_index_ids": ["ops-memory"],
    "events": [
      {
        "id": "evt-001",
        "type": "user",
        "turn_id": "turn-1",
        "event_at": "2026-11-16T09:58:00.000Z",
        "seq": 1,
        "content": "Why is checkout latency up since this morning?"
      },
      {
        "id": "evt-002",
        "type": "assistant",
        "turn_id": "turn-1",
        "event_at": "2026-11-16T09:58:02.000Z",
        "seq": 2,
        "tool_calls": [
          { "id": "call-1", "name": "search_logs", "args": { "index": "logs-checkout*", "since": "now-6h" } }
        ]
      },
      {
        "id": "evt-003",
        "type": "tool",
        "turn_id": "turn-1",
        "event_at": "2026-11-16T09:58:32.000Z",
        "seq": 3,
        "tool_call_id": "call-1",
        "tool_name": "search_logs",
        "status": "failed",
        "result": "Search timed out after 30 seconds",
        "duration_ms": 30000
      },
      {
        "id": "evt-004",
        "type": "assistant",
        "turn_id": "turn-1",
        "event_at": "2026-11-16T09:58:35.000Z",
        "seq": 4,
        "content": "The log search timed out, so I could not confirm it from the logs. The checkout-service deploy at 08:40 is the most likely cause. Check its connection pool settings, or roll it back."
      }
    ]
  }'
```

## Response examples

#### An event failed

`application/json`

Three events were saved. The last one could not be embedded and can be resent.

```json
{
  "results": [
    {
      "id": "evt-001",
      "status": "created"
    },
    {
      "id": "evt-002",
      "status": "created"
    },
    {
      "id": "evt-003",
      "status": "created"
    },
    {
      "error": "Exception when running inference id [.elser-2-elasticsearch] on field [content.semantic_text]",
      "id": "evt-004",
      "status": "failed"
    }
  ]
}
```

#### Every event saved

`application/json`

Each event was saved, in request order

```json
{
  "results": [
    {
      "id": "evt-001",
      "status": "created"
    },
    {
      "id": "evt-002",
      "status": "created"
    },
    {
      "id": "evt-003",
      "status": "created"
    },
    {
      "id": "evt-004",
      "status": "created"
    }
  ]
}
```

#### An AI index the caller cannot write to

`application/json`

The caller lacks the Elasticsearch `write` privilege on an AI index's backing index

```json
{
  "error": "Forbidden",
  "message": "AI index \u0027ops-memory\u0027 is not writable. Writing to it needs the Elasticsearch \u0027write\u0027 privilege on its backing index.",
  "statusCode": 403
}
```

#### Another user without manage memory

`application/json`

`user_id` names another user and the caller lacks `contextEngine:manage_memory`

```json
{
  "error": "Forbidden",
  "message": "Acting on the memory of another user requires the [contextEngine:manage_memory] privilege.",
  "statusCode": 403
}
```

#### No user to attribute the events to

`application/json`

`user_id` is missing and the caller has no user profile

```json
{
  "error": "Forbidden",
  "message": "The caller has no user profile, so the request cannot be attributed to a user. Log in to Kibana once to create a profile, use an API key whose owner has one, or set \u0060user_id\u0060 if you have the \u0060contextEngine:manage_memory\u0060 privilege.",
  "statusCode": 403
}
```

#### Unknown AI index

`application/json`

An AI index in `ai_index_ids` does not exist in this space

```json
{
  "error": "Not Found",
  "message": "AI index \u0027ops-memory\u0027 not found",
  "statusCode": 404
}
```

#### Context Engine turned off

`application/json`

Context Engine is turned off in this space

```json
{
  "error": "Not Found",
  "message": "Not Found",
  "statusCode": 404
}
```

#### Memory turned off

`application/json`

Context Engine memory is turned off

```json
{
  "error": "Not Found",
  "message": "Context Engine memory is turned off.",
  "statusCode": 404
}
```

