Problem Statement
I would like to optimize the session manager so that it doesnt call get_agent if the session does not already exist. This will reduce the number of api calls needed by the underlying session repository.
Proposed Solution
Add a flag during session manager initialization that skips get_agent call during agent init if the session doesnt already exist
Use Case
reduce number of api calls needed by session manager
Implementation Requirements
Based on repository analysis and clarification discussion:
Overview
When a new session is created in RepositorySessionManager.__init__, we know with certainty that no agents can exist in the session yet. The subsequent read_agent() calls in initialize(), initialize_bidi_agent(), and initialize_multi_agent() will always return None, making them unnecessary API calls.
This optimization eliminates redundant S3 GetObject calls (for S3-backed sessions) or filesystem I/O operations (for file-backed sessions).
Technical Approach
Add _is_new_session flag in RepositorySessionManager:
class RepositorySessionManager(SessionManager):
def __init__(self, session_id: str, session_repository: SessionRepository, **kwargs: Any):
# ... existing code ...
session = session_repository.read_session(session_id)
if session is None:
self._is_new_session = True # NEW: Track new session state
session = Session(session_id=session_id, session_type=SessionType.AGENT)
session_repository.create_session(session)
else:
self._is_new_session = False # Session existed, agents may exist
# ... rest of init ...
Update initialization methods to check flag:
In initialize(), initialize_bidi_agent(), and initialize_multi_agent():
- If
self._is_new_session is True, skip the read_agent()/read_multi_agent() call
- Directly proceed to create the agent/multi-agent
- The flag remains
True for the session's lifetime (agents can't pre-exist in a new session)
Files to Modify
| File |
Changes |
src/strands/session/repository_session_manager.py |
Add _is_new_session flag and update initialize(), initialize_bidi_agent(), initialize_multi_agent() methods |
tests/strands/session/test_repository_session_manager.py |
Add tests for optimization behavior |
Acceptance Criteria
Design Notes
- The
_is_new_session flag remains True for the entire lifetime of the session manager instance
- This is safe because: if the session was newly created, no agents could have pre-existed in the repository
- The same session manager cannot be attached to agents with the same agent_id (enforced by existing validation)
Problem Statement
I would like to optimize the session manager so that it doesnt call
get_agentif the session does not already exist. This will reduce the number of api calls needed by the underlying session repository.Proposed Solution
Add a flag during session manager initialization that skips
get_agentcall during agent init if the session doesnt already existUse Case
reduce number of api calls needed by session manager
Implementation Requirements
Based on repository analysis and clarification discussion:
Overview
When a new session is created in
RepositorySessionManager.__init__, we know with certainty that no agents can exist in the session yet. The subsequentread_agent()calls ininitialize(),initialize_bidi_agent(), andinitialize_multi_agent()will always returnNone, making them unnecessary API calls.This optimization eliminates redundant S3 GetObject calls (for S3-backed sessions) or filesystem I/O operations (for file-backed sessions).
Technical Approach
Add
_is_new_sessionflag inRepositorySessionManager:Update initialization methods to check flag:
In
initialize(),initialize_bidi_agent(), andinitialize_multi_agent():self._is_new_sessionisTrue, skip theread_agent()/read_multi_agent()callTruefor the session's lifetime (agents can't pre-exist in a new session)Files to Modify
src/strands/session/repository_session_manager.py_is_new_sessionflag and updateinitialize(),initialize_bidi_agent(),initialize_multi_agent()methodstests/strands/session/test_repository_session_manager.pyAcceptance Criteria
RepositorySessionManager.__init__sets_is_new_session = Truewhen creating a new sessionRepositorySessionManager.__init__sets_is_new_session = Falsewhen using an existing sessioninitialize(agent)skipsread_agent()call when_is_new_sessionisTrueinitialize_bidi_agent(agent)skipsread_agent()call when_is_new_sessionisTrueinitialize_multi_agent(source)skipsread_multi_agent()call when_is_new_sessionisTrue_is_new_sessionisFalse(session already existed)Design Notes
_is_new_sessionflag remainsTruefor the entire lifetime of the session manager instance