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

# Session • Rust AI Agent SDK

> Session: Session manager - main API for session persistence

# Session

> Defined in the [**session**](../modules/session) module.

<Badge color="orange">Rust AI Agent SDK</Badge>

Session manager - main API for session persistence

## Fields

| Name         | Type                          | Description |
| ------------ | ----------------------------- | ----------- |
| `session_id` | `String`                      | -           |
| `data`       | `SessionData`                 | -           |
| `store`      | `Arc&lt;dyn SessionStore&gt;` | -           |

## Methods

### `new`

```rust theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
fn new(session_id: impl Into<String>) -> Self
```

Create a new session with default file store

**Parameters:**

| Name         | Type                      |
| ------------ | ------------------------- |
| `session_id` | `impl Into&lt;String&gt;` |

### `with_store`

```rust theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
fn with_store(session_id: impl Into<String>, store: Arc<dyn SessionStore>) -> Self
```

Create with custom store

**Parameters:**

| Name         | Type                          |
| ------------ | ----------------------------- |
| `session_id` | `impl Into&lt;String&gt;`     |
| `store`      | `Arc&lt;dyn SessionStore&gt;` |

### `load`

```rust theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
fn load(session_id: impl Into<String>) -> Result<Self>
```

Load an existing session

**Parameters:**

| Name         | Type                      |
| ------------ | ------------------------- |
| `session_id` | `impl Into&lt;String&gt;` |

### `id`

```rust theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
fn id(&self) -> &str
```

Get session ID

### `add_user_message`

```rust theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
fn add_user_message(&mut self, content: impl Into<String>) -> Result<()>
```

Add a user message

**Parameters:**

| Name      | Type                      |
| --------- | ------------------------- |
| `content` | `impl Into&lt;String&gt;` |

### `add_assistant_message`

```rust theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
fn add_assistant_message(&mut self, content: impl Into<String>) -> Result<()>
```

Add an assistant message

**Parameters:**

| Name      | Type                      |
| --------- | ------------------------- |
| `content` | `impl Into&lt;String&gt;` |

### `add_message`

```rust theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
fn add_message(&mut self, role: &str, content: impl Into<String>) -> Result<()>
```

Add a message with role

**Parameters:**

| Name      | Type                      |
| --------- | ------------------------- |
| `role`    | `&str`                    |
| `content` | `impl Into&lt;String&gt;` |

### `get_history`

```rust theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
fn get_history(&self, max_messages: Option<usize>) -> Vec<Message>
```

Get chat history as LLM messages

**Parameters:**

| Name           | Type                  |
| -------------- | --------------------- |
| `max_messages` | `Option&lt;usize&gt;` |

### `messages`

```rust theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
fn messages(&self) -> &[SessionMessage]
```

Get all messages

### `message_count`

```rust theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
fn message_count(&self) -> usize
```

Get message count

### `set_agent_name`

```rust theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
fn set_agent_name(&mut self, name: impl Into<String>) -> Result<()>
```

Set agent name

**Parameters:**

| Name   | Type                      |
| ------ | ------------------------- |
| `name` | `impl Into&lt;String&gt;` |

### `set_user_id`

```rust theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
fn set_user_id(&mut self, user_id: impl Into<String>) -> Result<()>
```

Set user ID

**Parameters:**

| Name      | Type                      |
| --------- | ------------------------- |
| `user_id` | `impl Into&lt;String&gt;` |

### `clear`

```rust theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
fn clear(&mut self) -> Result<()>
```

Clear all messages

### `delete`

```rust theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
fn delete(self) -> Result<()>
```

Delete the session

### `exists`

```rust theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
fn exists(&self) -> bool
```

Check if session exists on disk

### `save`

```rust theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
fn save(&self) -> Result<()>
```

Save current state

## Source

<Card title="View on GitHub" icon="github" href="https://github.com/MervinPraison/PraisonAI/blob/main/src/praisonai-rust/praisonai/src/session/mod.rs#L440">
  `praisonai/src/session/mod.rs` at line 440
</Card>

***

## Related Documentation

<CardGroup cols={2}>
  <Card title="Rust Sessions" icon="clock" href="/docs/rust/sessions" />

  <Card title="Rust Memory" icon="database" href="/docs/rust/memory" />
</CardGroup>
