Skip to content
Back to skills

290 Session Management C88b5c28

ASecurity

{{ experimental_feature_warning() }} Session management for `BidiAgent` provides a mechanism for persisting conversation history and agent state across bidirectional streaming sessions. This enables voice assistants and interactive applications to maintain context and continuity even when connections are restarted or the application is redeployed.

  • 9 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added May 31, 2026
documentationpythongotestingapidocumentation

Works with

  • api

Security analysis

A100/100

Pro scans all 2 files and shows the line behind each finding

Scanned May 31, 2026

npx -y skills add tools-only/X-Skills --skill 290-session-management_c88b5c28 --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of 290 Session Management C88b5c28?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for 290 Session Management C88b5c28
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/tools-only-290-session-management-c88b5c28/badge)](https://www.skillsdirectory.com/skills/tools-only-290-session-management-c88b5c28)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
# Session Management [Experimental]

{{ experimental_feature_warning() }}

Session management for `BidiAgent` provides a mechanism for persisting conversation history and agent state across bidirectional streaming sessions. This enables voice assistants and interactive applications to maintain context and continuity even when connections are restarted or the application is redeployed.

## Overview

A bidirectional streaming session represents all stateful information needed by the agent to function, including:

- Conversation history (messages with audio transcripts)
- Agent state (key-value storage)
- Connection state and configuration
- Tool execution history

Strands provides built-in session persistence capabilities that automatically capture and restore this information, allowing `BidiAgent` to seamlessly continue conversations where they left off, even after connection timeouts or application restarts.

For a comprehensive introduction to session management concepts and general patterns, see the [Session Management documentation](../agents/session-management.md). This guide focuses on bidirectional streaming-specific considerations and use cases.

## Basic Usage

Create a `BidiAgent` with a session manager and use it:

```python
from strands.experimental.bidi import BidiAgent, BidiAudioIO
from strands.experimental.bidi.models import BidiNovaSonicModel
from strands.session.file_session_manager import FileSessionManager

# Create a session manager with a unique session ID
session_manager = FileSessionManager(session_id="user_123_voice_session")

# Create the agent with session management
model = BidiNovaSonicModel()
agent = BidiAgent(
    model=model,
    session_manager=session_manager
)

# Use the agent - all messages are automatically persisted
audio_io = BidiAudioIO()
await agent.run(
    inputs=[audio_io.input()],
    outputs=[audio_io.output()]
)
```

The conversation history is automatically persisted and will be restored on the next session.

## Provider-Specific Considerations

### Gemini Live

!!! warning "Limited Session Management Support"
    Gemini Live does not yet have full session management support due to message history recording limitations in the current implementation. For connection restarts, Gemini Live uses Google's [session handlers](https://ai.google.dev/gemini-api/docs/live-session) to maintain conversation continuity within a single session, but conversation history is not persisted across application restarts.

When using Gemini Live with connection restarts, the model leverages Google's built-in session handler mechanism to maintain context during reconnections within the same session lifecycle.

## Built-in Session Managers

Strands offers two built-in session managers for persisting bidirectional streaming sessions:

1. **FileSessionManager**: Stores sessions in the local filesystem
2. **S3SessionManager**: Stores sessions in Amazon S3 buckets

### FileSessionManager

The `FileSessionManager` provides a simple way to persist sessions to the local filesystem:

```python
from strands.experimental.bidi import BidiAgent
from strands.session.file_session_manager import FileSessionManager

# Create a session manager
session_manager = FileSessionManager(
    session_id="user_123_session",
    storage_dir="/path/to/sessions"  # Optional, defaults to temp directory
)

agent = BidiAgent(
    model=model,
    session_manager=session_manager
)
```

**Use cases:**

- Development and testing
- Single-server deployments
- Local voice assistants
- Prototyping

### S3SessionManager

The `S3SessionManager` stores sessions in Amazon S3 for distributed deployments:

```python
from strands.experimental.bidi import BidiAgent
from strands.session.s3_session_manager import S3SessionManager

# Create an S3 session manager
session_manager = S3SessionManager(
    session_id="user_123_session",
    bucket="my-voice-sessions",
    prefix="sessions/"  # Optional prefix for organization
)

agent = BidiAgent(
    model=model,
    session_manager=session_manager
)
```

**Use cases:**

- Production deployments
- Multi-server environments
- Serverless applications
- High availability requirements

## Session Lifecycle

### Session Creation

Sessions are created automatically when the agent starts:

```python
session_manager = FileSessionManager(session_id="new_session")
agent = BidiAgent(model=model, session_manager=session_manager)

# Session created on first start
await agent.start()
```

### Session Restoration

When an agent starts with an existing session ID, the conversation history is automatically restored:

```python
# First conversation
session_manager = FileSessionManager(session_id="user_123")
agent = BidiAgent(model=model, session_manager=session_manager)
await agent.start()
await agent.send("My name is Alice")
# ... conversation continues ...
await agent.stop()

# Later - conversation history restored
session_manager = FileSessionManager(session_id="user_123")
agent = BidiAgent(model=model, session_manager=session_manager)
await agent.start()  # Previous messages automatically loaded
await agent.send("What's my name?")  # Agent remembers: "Alice"
```

### Session Updates

Messages are persisted automatically as they're added:

```python
agent = BidiAgent(model=model, session_manager=session_manager)
await agent.start()

# Each message automatically saved
await agent.send("Hello")  # Saved
# Model response received and saved
# Tool execution saved
# All transcripts saved
```

## Connection Restart Behavior

When a connection times out and restarts, the session manager ensures continuity:

```python
agent = BidiAgent(model=model, session_manager=session_manager)
await agent.start()

async for event in agent.receive():
    if isinstance(event, BidiConnectionRestartEvent):
        # Connection restarting due to timeout
        # Session manager ensures:
        # 1. All messages up to this point are saved
        # 2. Full history sent to restarted connection
        # 3. Conversation continues seamlessly
        print("Reconnecting with full history preserved")
```

## Integration with Hooks

Session management works seamlessly with hooks:

```python
from strands.experimental.bidi.hooks.events import BidiMessageAddedEvent

class SessionLogger:
    async def on_message_added(self, event: BidiMessageAddedEvent):
        # Message already persisted by session manager
        print(f"Message persisted: {event.message['role']}")

agent = BidiAgent(
    model=model,
    session_manager=session_manager,
    hooks=[SessionLogger()]
)
```

The `BidiMessageAddedEvent` is emitted after the message is persisted, ensuring hooks see the saved state.

For best practices on session ID management, session cleanup, error handling, storage considerations, and troubleshooting, see the [Session Management documentation](../agents/session-management.md).

## Next Steps

- [Agent](agent.md) - Learn about BidiAgent configuration and lifecycle
- [Hooks](hooks.md) - Extend agent functionality with hooks
- [Events](events.md) - Complete guide to bidirectional streaming events
- [API Reference](../../../api-reference/python/experimental/bidi/agent/agent.md) - Complete API documentation

Files in this skill

  • README.md897 B
  • skill.md7 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.