Skip to main content
Short-term memory stores messages, tool interactions, and state so later turns can restore context. Sessions are identified by 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

If a username or password in the connection string contains special characters such as @ or :, URL-encode it with urllib.parse.quote_plus first (e.g. p@ssword → p%40ssword), otherwise parsing fails.

Backend behavior

All backends provide the same session-management operations. They differ in persistence location and connection method:
  • local keeps sessions in the current process only;
  • sqlite writes sessions to a local file;
  • mysql and postgresql write sessions to an external database for multi-instance sharing.

Choosing a backend

Working with the Runner

Short-term memory is usually passed to the Runner, 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 a Session directly; instead you use session_service to manage the full lifecycle:
  • Start a session create_session(): create a new Session when the user starts interacting.
  • Resume a session get_session(): retrieve a Session by session_id to 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 a Session and its associated data.
A session-creation callback lets you provision resources, record an audit event, or synchronize an external system after a session is first created. Register after_create_session_callback:
The callback runs only when a new session is actually created; it is not invoked when an existing session is reused. The callback may be a synchronous function or an asynchronous function (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 the App 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

Use LlmEventSummarizer 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):
A custom template must contain {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
Last modified on September 19, 2026