app_name, user_id, and session_id; continuing a session requires the same storage backend and all three identifiers
The agent configuration supplies system instructions. Compaction, filtering, and agent settings also determine which history reaches the model. Persisting a session does not mean sending its entire history unchanged on every turn
Short-term memory builds on Google ADK’s Session mechanism. For background, see Google ADK Session.
Single entry point: ShortTermMemory
Whatever backend you use, you interact with one class —veadk.memory.short_term_memory.ShortTermMemory. It selects the storage mode based on backend (or db_url) and exposes a uniform session-management surface.
Parameters
Backend behavior
All backends provide the same session-management operations. They differ in persistence location and connection method:localkeeps sessions in the current process only;sqlitewrites sessions to a local file;mysqlandpostgresqlwrite sessions to an external database for multi-instance sharing.
Choosing a backend
Working with the Runner
Short-term memory is usually passed to theRunner, which then creates or restores sessions automatically. Complete model configuration, then reuse the app, user, and session identifiers to continue the context
If you pass neither
short_term_memory nor session_service to the Runner, it falls back to creating a local (in-memory) short-term memory automatically.Session management interface
You rarely create or manage aSession directly; instead you use session_service to manage the full lifecycle:
- Start a session
create_session(): create a newSessionwhen the user starts interacting. - Resume a session
get_session(): retrieve aSessionbysession_idto continue. - Save progress
append_event(): append a new interaction (Event) to the history. - List sessions
list_sessions(): query active sessions for a user and app. - Clean up
delete_session(): delete aSessionand its associated data.
after_create_session_callback:
async def). If it raises an exception,
the exception is propagated to the caller and agent execution does not continue.
For every backend,
ShortTermMemory.create_session() first checks for
and reuses an existing session with the same app_name, user_id, and session_id to avoid duplicates.
The callback only runs on first creation.Context compaction
As a conversation grows, the history keeps expanding, increasing what the model must process and slowing responses. Context compaction summarizes the history with a sliding window: when the history exceeds a threshold, older events are compacted automatically.Configure compaction
Pass theApp configuration below to a Google ADK Runner or app loader that supports it to compact history at the configured invocation interval. Creating an App alone does not run compaction.
Custom compactor
UseLlmEventSummarizer to set the model and prompt template for compaction. Provide the model’s API key and base via environment variables (read from os.environ, never hard-code):
{conversation_history}; otherwise the summary request contains no history to summarize. The app above is application configuration and must be passed to a Google ADK Runner or app loader that supports App. Constructing it alone does not run conversations or compaction. Compaction makes additional model requests, and summaries may omit detail, so they are not backups of the original conversation
after_load_memory_callback must be synchronous, may receive None for a missing session, and should accept query keyword arguments. after_create_session_callback runs only for sessions created through ShortTermMemory.create_session(); direct session_service.create_session() calls bypass it. A failed callback does not automatically remove the session that was already created