Save session events Experimental; added in 9.7.0

POST /api/context_engine/memory/_save_event

Spaces method and path for this operation:

post /s/{space_id}/api/context_engine/memory/_save_event

Refer to Spaces for more information.

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.

[Required authorization] Route required privileges: contextEngine:write. Extended privileges (optional, expand functionality when granted): contextEngine:manage_memory.

Headers

  • kbn-xsrf string Required

    A required header to protect against CSRF attacks

application/json

Body

  • ai_index_ids array[string]

    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.

    Not more than 100 elements. Minimum length of each is 1, maximum length of each is 256.

  • events array[object] Required

    The events to save.

    At least 1 but not more than 1000 elements.

    One of:
  • session_id string Required

    The session the events belong to.

    Minimum length is 1, maximum length is 256.

  • user_id string

    The Kibana user profile UID that the events belong to. Defaults to the caller's user profile. Naming another user requires contextEngine:manage_memory.

    Minimum length is 1, maximum length is 1024.

Responses

  • 200 application/json

    The result for every event.

    Hide response attribute Show response attribute object
    • results array[object] Required

      One result per event, in request order.

      Not more than 1000 elements.

      Hide results attributes Show results attributes object
      • error string

        Why the event failed. Resend it to retry.

      • id string Required

        The event id.

      • status string Required

        Whether the event was stored.

        Values are created or failed.

  • 403 application/json

    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.

    Hide response attributes Show response attributes object
    • error string

      The HTTP status text.

    • message string Required

      A human-readable error message.

    • statusCode number

      The HTTP status code.

  • 404 application/json

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

    Hide response attributes Show response attributes object
    • error string

      The HTTP status text.

    • message string Required

      A human-readable error message.

    • statusCode number

      The HTTP status code.

POST /api/context_engine/memory/_save_event
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."
      }
    ]
  }'
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."
    }
  ]
}
Request example
Example request to save one turn: the user's question, a tool call that fails, and the final answer. The tool event's `result` and `duration_ms` are stored as sent.
{
  "ai_index_ids": [
    "ops-memory"
  ],
  "events": [
    {
      "content": "Why is checkout latency up since this morning?",
      "event_at": "2026-11-16T09:58:00.000Z",
      "id": "evt-001",
      "seq": 1,
      "turn_id": "turn-1",
      "type": "user"
    },
    {
      "event_at": "2026-11-16T09:58:02.000Z",
      "id": "evt-002",
      "seq": 2,
      "tool_calls": [
        {
          "args": {
            "index": "logs-checkout*",
            "since": "now-6h"
          },
          "id": "call-1",
          "name": "search_logs"
        }
      ],
      "turn_id": "turn-1",
      "type": "assistant"
    },
    {
      "duration_ms": 30000,
      "event_at": "2026-11-16T09:58:32.000Z",
      "id": "evt-003",
      "result": "Search timed out after 30 seconds",
      "seq": 3,
      "status": "failed",
      "tool_call_id": "call-1",
      "tool_name": "search_logs",
      "turn_id": "turn-1",
      "type": "tool"
    },
    {
      "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.",
      "event_at": "2026-11-16T09:58:35.000Z",
      "id": "evt-004",
      "seq": 4,
      "turn_id": "turn-1",
      "type": "assistant"
    }
  ],
  "session_id": "3f9c2e1a-7b4d-4c8e-9a61-5d2f0b7e8c43"
}
Response examples (200)
Three events were saved. The last one could not be embedded and can be resent.
{
  "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"
    }
  ]
}
Each event was saved, in request order
{
  "results": [
    {
      "id": "evt-001",
      "status": "created"
    },
    {
      "id": "evt-002",
      "status": "created"
    },
    {
      "id": "evt-003",
      "status": "created"
    },
    {
      "id": "evt-004",
      "status": "created"
    }
  ]
}
Response examples (403)
The caller lacks the Elasticsearch `write` privilege on an AI index's backing index
{
  "error": "Forbidden",
  "message": "AI index 'ops-memory' is not writable. Writing to it needs the Elasticsearch 'write' privilege on its backing index.",
  "statusCode": 403
}
`user_id` names another user and the caller lacks `contextEngine:manage_memory`
{
  "error": "Forbidden",
  "message": "Acting on the memory of another user requires the [contextEngine:manage_memory] privilege.",
  "statusCode": 403
}
`user_id` is missing and the caller has no user profile
{
  "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 `user_id` if you have the `contextEngine:manage_memory` privilege.",
  "statusCode": 403
}
Response examples (404)
An AI index in `ai_index_ids` does not exist in this space
{
  "error": "Not Found",
  "message": "AI index 'ops-memory' not found",
  "statusCode": 404
}
Context Engine is turned off in this space
{
  "error": "Not Found",
  "message": "Not Found",
  "statusCode": 404
}
Context Engine memory is turned off
{
  "error": "Not Found",
  "message": "Context Engine memory is turned off.",
  "statusCode": 404
}