@@ -218,6 +218,7 @@ result = agent_with_tools("/path/to/image.png")
218218- ` session_id ` : Unique identifier for the conversation session
219219- ` actor_id ` : Unique identifier for the user/actor
220220- ` retrieval_config ` : Dictionary mapping namespaces to RetrievalConfig objects
221+ - ` batch_size ` : Number of messages to buffer before sending to AgentCore Memory (1-100, default: 1). A value of 1 sends immediately (no batching).
221222
222223### RetrievalConfig Parameters
223224
@@ -238,6 +239,55 @@ https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/memory-strategies.
238239- ` /summaries/{actorId}/{sessionId}/ ` : Session-specific summaries
239240
240241
242+ ---
243+
244+ ## Message Batching
245+
246+ When ` batch_size ` is greater than 1, messages are buffered in memory and sent to AgentCore Memory
247+ in a single API call once the buffer reaches the configured size. This reduces the number of API
248+ requests in high-throughput conversations.
249+
250+ > ** Important:** When using ` batch_size > 1 ` , you ** must** use a ` with ` block or call ` close() `
251+ > when the session is complete. Otherwise, any buffered messages that have not yet reached the
252+ > batch threshold will be lost.
253+
254+ ### Recommended: Context Manager
255+
256+ ``` python
257+ config = AgentCoreMemoryConfig(
258+ memory_id = MEM_ID ,
259+ session_id = SESSION_ID ,
260+ actor_id = ACTOR_ID ,
261+ batch_size = 10 , # Buffer up to 10 messages before sending
262+ )
263+
264+ # The `with` block guarantees all buffered messages are flushed on exit
265+ with AgentCoreMemorySessionManager(config, region_name = ' us-east-1' ) as session_manager:
266+ agent = Agent(
267+ system_prompt = " You are a helpful assistant." ,
268+ session_manager = session_manager,
269+ )
270+ agent(" Hello!" )
271+ agent(" Tell me about AWS" )
272+ # All remaining buffered messages are automatically flushed here
273+ ```
274+
275+ ### Alternative: Explicit close()
276+
277+ If you cannot use a ` with ` block, call ` close() ` manually:
278+
279+ ``` python
280+ session_manager = AgentCoreMemorySessionManager(config, region_name = ' us-east-1' )
281+ try :
282+ agent = Agent(
283+ system_prompt = " You are a helpful assistant." ,
284+ session_manager = session_manager,
285+ )
286+ agent(" Hello!" )
287+ finally :
288+ session_manager.close() # Flush any remaining buffered messages
289+ ```
290+
241291---
242292
243293## Important Notes
@@ -255,3 +305,4 @@ https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/memory-strategies.
255305- Use consistent ` actor_id ` for the same user across sessions
256306- Configure appropriate ` relevance_score ` thresholds for your use case
257307- Test with different ` top_k ` values to optimize retrieval performance
308+ - When using ` batch_size > 1 ` , always use a ` with ` block or call ` close() ` to ensure buffered messages are flushed before the session ends
0 commit comments