> ## Documentation Index
> Fetch the complete documentation index at: https://platform.kimi.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Create a Session

> Create a session to run an agent, bind the required execution environment and resources, and send the first message to start a task.

A **session** is a single running instance of an agent for a specific task.
When you create a session, the platform pins the specified agent version, execution environment version, and session resources, and returns a session ID prefixed with `sesn_`.
You can then use this ID to send and subscribe to events and manage the session lifecycle.

> [!NOTE]
>
> Before creating a session, prepare an [agent](https://platform.kimi.ai/docs/hosted-agents/agents.md) and an available [execution environment](https://platform.kimi.ai/docs/hosted-agents/environments.md).

> [!WARNING]
>
> **Creating a session does not start a task.** Newly created sessions start in the `idle` status—the agent does not run and no task events are emitted yet.
> We recommend opening the [event stream](https://platform.kimi.ai/docs/hosted-agents/event-stream.md) first and then sending the first `user.message`; the session enters the `running` status only after receiving that input.

## Session configuration

In addition to the API, you can create sessions, send tasks, monitor progress, and download results through the [Console](https://platform.kimi.ai/docs/hosted-agents/console.md#sessions) or the [kha CLI](https://platform.kimi.ai/docs/hosted-agents/kha.md). The examples below show both cURL and kha CLI commands.

Send a `POST /v1/sessions` request to create a session.
Only `agent_id` and `environment_id` are required in the request body:

| Field | Type | Required | Description |
| - | - | - | - |
| `agent_id` | `string` | Yes | ID of the agent to run. |
| `environment_id` | `string` | Yes | ID of the execution environment for the session. The environment must be visible to the caller, must not be archived, and must have at least one `ready` version. It cannot be changed after creation. |
| `agent_version` | `string` | No | Exact agent version to use. If omitted, pins the latest version available at creation time. |
| `title` | `string` | No | Session title, up to 256 characters; can be updated after creation. |
| `metadata` | `object` | No | Custom key-value pairs for filtering and annotation, up to 16 entries. |
| `agent_overrides` | `object` | No | Per-session overrides for the agent configuration. Supports `mcp_servers`, `skills`, `plugins`, `tools`, `model`, and `system`; `roster` can also be set at creation time. |
| `resources` | `array[object]` | No | File, vault, memory store, or GitHub repository resources bound at creation time. You can bind at most 16 vaults and 8 memory stores to a session; in the current implementation, each session can bind at most 4 GitHub repositories by default. |

`agent_overrides` customizes the configuration of the pinned agent version for the current session without modifying the agent resource or creating a new agent version.
`roster` can only be set when creating a session, and its entries are resolved and pinned to exact versions at creation time; passing `roster` when updating a session returns HTTP 400 `invalid_parameter_error`. The provider of `model` must match the session's model provider.

Each collection field supports three states: omitting the field inherits the collection from the pinned agent version on creation (leaving the existing override unchanged on update), passing an explicit empty array clears the collection for this session on creation (clearing the override and falling back to the pinned agent version's collection on update), and passing a non-empty array replaces the entire collection rather than merging with the existing one.
Custom tool declarations and validation rules for `tools` match those on an agent, with one exception for built-in toolsets: if the `tools` array you pass does not include an `agent_toolset`, the platform keeps the `agent_toolset` declaration from the bound agent (or the currently effective one), or prepends `agent_toolset_20261101` if none exists. When updating a session, passing `tools: []` clears the tools override and inherits the bound agent's tools. Each `tools` list allows at most one sandbox toolset.
These settings affect only the current session.

Each object in `resources` uses `type` to specify the resource kind:

* `file`: References an uploaded file via `file_id`.
* `vault`: Binds a vault via `vault_id`. Bind it at session creation; it cannot be added or modified later, but you can unbind it via the session resources endpoints.
* `memory_store`: Binds a memory store via `memory_store_id` and sets `access` to `read_only` or `read_write`. Bind it at session creation; it cannot be added or modified later, but you can unbind the store via the session resources endpoints. Optional `instructions` provide additional guidance to the model for this binding.
* `github_repository`: Specifies a GitHub repository via `url`, with optional `checkout`, `mount_path`, and `authorization_token` for private repositories; the binding cannot be added, modified, or removed after creation.

Files, vaults, and memory stores are bound by reference—file contents and credential values are not copied into the request body. GitHub repositories are cloned into the session sandbox according to their `url` and `checkout` configuration.
To prepare files, vaults, memory stores, and GitHub repositories, see [Files](https://platform.kimi.ai/docs/hosted-agents/files.md), [Vaults](https://platform.kimi.ai/docs/hosted-agents/vaults.md), [Memory](https://platform.kimi.ai/docs/hosted-agents/memory.md), and [GitHub Repositories](https://platform.kimi.ai/docs/hosted-agents/github.md).

## Create a session

The following request creates a session and binds an uploaded file.
Omit `resources` if you do not need to bind any resources; to bind vaults, memory stores, or GitHub repositories, add the corresponding configurations to the same array.

```bash cURL theme={null}
curl "https://api.moonshot.ai/v1/sessions" \
  -H "Authorization: Bearer $KIMI_API_KEY" \
  -H "kimi-api-version: 2026-09-01-beta" \
  -H "Content-Type: application/json" \
  -d '{
    "agent_id": "agent_your_agent_id",
    "environment_id": "env_your_environment_id",
    "agent_version": "version_you_want_to_pin",
    "title": "Repository research task",
    "metadata": {
      "project": "backend-migration"
    },
    "agent_overrides": {
      "mcp_servers": [
        {
          "type": "url",
          "name": "project-tools",
          "url": "https://mcp.example.com/mcp"
        }
      ],
      "skills": [
        {
          "skill_id": "skill_your_skill_id",
          "version": "your_skill_version"
        }
      ]
    },
    "resources": [
      {
        "type": "file",
        "file_id": "file_your_file_id"
      }
    ]
  }'
```

```bash CLI theme={null}
kha sessions create --file - <<'JSON'
{
  "agent_id": "agent_your_agent_id",
  "environment_id": "env_your_environment_id",
  "agent_version": "version_you_want_to_pin",
  "title": "Repository research task",
  "metadata": {
    "project": "backend-migration"
  },
  "agent_overrides": {
    "mcp_servers": [
      {
        "type": "url",
        "name": "project-tools",
        "url": "https://mcp.example.com/mcp"
      }
    ],
    "skills": [
      {
        "skill_id": "skill_your_skill_id",
        "version": "your_skill_version"
      }
    ]
  },
  "resources": [
    {
      "type": "file",
      "file_id": "file_your_file_id"
    }
  ]
}
JSON
```

When creation succeeds, the endpoint responds with the session object.
At this point `status` is `idle`, meaning the session has been created but has not started running yet:

```json theme={null}
{
  "id": "sesn_your_session_id",
  "agent_id": "agent_your_agent_id",
  "environment_id": "env_your_environment_id",
  "status": "idle",
  "title": "Repository research task",
  "metadata": {
    "project": "backend-migration"
  },
  "resources": [
    {
      "type": "file",
      "id": "sres_your_resource_id",
      "file_id": "file_your_file_id"
    }
  ],
  "created_at": "2026-01-01T00:00:00Z",
  "updated_at": "2026-01-01T00:00:00Z"
}
```

## Versions pinned at creation

When you create a session, the platform selects and pins the following configuration:

* **Agent version**: If `agent_version` is omitted, the platform pins the latest version available at creation time; if specified, it pins that exact version.
  Updating the agent later does not change the version used by this session.
* **Execution environment version**: The platform pins the latest `ready` version of the specified environment at creation time.
  `environment_id` cannot be changed once bound; if the environment is not visible, is archived, or has no `ready` version, the creation request is rejected.
* **Session resources**: Vault, memory store, and GitHub repository bindings can only be created with the session and cannot be added or modified later. You can unbind a vault or memory store via `DELETE /v1/sessions/{id}/resources/{resource_id}`: unbinding a vault stops credential injection immediately, while unbinding a memory store takes effect on the next wake-up (the in-flight turn is unaffected). GitHub repository bindings cannot be removed after creation.
  Files can be added or removed after creation via the session resources endpoints; see [Files](https://platform.kimi.ai/docs/hosted-agents/files.md). For repository configuration, see [GitHub Repositories](https://platform.kimi.ai/docs/hosted-agents/github.md).

> [!TIP]
>
> To run a new task with an updated agent or execution environment configuration, create a new session.
> Existing sessions continue using their pinned versions.

## Start a task

After creating a session, start a task in the following order:

**1. Subscribe to the event stream**

Call `GET /v1/sessions/{session_id}/events/stream` to open an SSE connection so you receive status updates and output events once execution starts.

**2. Send the first message**

Call `POST /v1/sessions/{session_id}/events` to send a `user.message` event.
This request adds the task to the session input queue and triggers execution.

**3. Process events**

Listen for `session.status`, `agent.message`, and other events until the current turn finishes.
For event schemas and stream resumption after a disconnect, see [Event Stream](https://platform.kimi.ai/docs/hosted-agents/event-stream.md).

Run the examples below in two separate terminals. Subscribe to the event stream in Terminal 1 first, and then send the first message in Terminal 2; replace `sesn_your_session_id` with the session ID returned when you created the session.

**Terminal 1: Subscribe to the event stream**

```bash cURL theme={null}
curl -sS -N --request GET \
  "https://api.moonshot.ai/v1/sessions/sesn_your_session_id/events/stream?cursor=now" \
  --header "Authorization: Bearer $KIMI_API_KEY" \
  --header "kimi-api-version: 2026-09-01-beta" \
  --header "Accept: text/event-stream"
```

```bash CLI theme={null}
kha sessions attach sesn_your_session_id --since now
```

**Terminal 2: Send the first message**

```bash cURL theme={null}
curl --request POST \
  "https://api.moonshot.ai/v1/sessions/sesn_your_session_id/events" \
  --header "Authorization: Bearer $KIMI_API_KEY" \
  --header "kimi-api-version: 2026-09-01-beta" \
  --header "Content-Type: application/json" \
  --data '{
    "events": [
      {
        "type": "user.message",
        "data": {
          "content": [
            {
              "type": "text",
              "text": "Analyze this dataset and summarize your findings."
            }
          ]
        }
      }
    ]
  }'
```

```bash CLI theme={null}
kha sessions send sesn_your_session_id "Analyze this dataset and summarize your findings."
```

After you send the message, events stream continuously in Terminal 1. As `session.status`, `agent.message`, and other events arrive, you can monitor task status and agent output. For disconnect recovery and event handling details, see [Event Stream](https://platform.kimi.ai/docs/hosted-agents/event-stream.md).

## Next steps

> [card] [Subscribe to Session Events](https://platform.kimi.ai/docs/hosted-agents/event-stream.md)
>
> Open an SSE connection, send messages, and process real-time events.

> [card] [Cancel, Archive, or Delete Sessions](https://platform.kimi.ai/docs/hosted-agents/session-operations.md)
>
> Inspect sessions, update titles, cancel turns, or archive and delete sessions.
