agentscope-java icon indicating copy to clipboard operation
agentscope-java copied to clipboard

[help wanted]: Mem0LongTermMemory does not support dynamic user switching per request

Open z43460018724 opened this issue 6 months ago • 4 comments

Issue: Mem0LongTermMemory does not support dynamic user switching per request Description Currently, Mem0LongTermMemory binds a fixed userId at construction time, which cannot be changed per request. This design makes it difficult to implement multi-tenant scenarios where a single ReActAgent instance should serve multiple users with isolated long-term memories.

Current Behavior java // User ID is fixed at construction time Mem0LongTermMemory memory = Mem0LongTermMemory.builder() .userId("fixed-user-123") // Cannot change per request .apiBaseUrl("...") .build();

ReActAgent agent = ReActAgent.builder() .longTermMemory(memory) // Memory is bound to a single user .build();

z43460018724 avatar May 20 '26 08:05 z43460018724

You can build Mem0LongTermMemory instance per sessionId and userId.

hexly666 avatar May 20 '26 16:05 hexly666

@hexly666 Thanks for your input! Just to confirm, is this the intended design approach?

Core approach: Create a separate Mem0LongTermMemory instance and ReActAgent instance for each (userId, sessionId) combination.

Complete Code Example

@Service
public class ChatService {
    
    @Autowired
    private OpenAIChatModel sharedModel;  // Shared model instance
    
    public Mono<String> chat(String userId, String sessionId, String message) {
        // Create dedicated memory instance for each session+user
        Mem0LongTermMemory userMemory = Mem0LongTermMemory.builder()
            .userId(userId)                    // User isolation
            .runName(sessionId)                // Session isolation
            .agentName("Assistant")
            .apiBaseUrl("https://api.mem0.ai")
            .apiKey(System.getenv("MEM0_API_KEY"))
            .build();
        
        // Agent is lightweight, per-request creation is acceptable
        ReActAgent agent = ReActAgent.builder()
            .name("Assistant")
            .model(sharedModel)                // Reuse shared model
            .longTermMemory(userMemory)
            .longTermMemoryMode(LongTermMemoryMode.STATIC_CONTROL)
            .build();
        
        return agent.call(message).map(Msg::getTextContent);
    }
}

z43460018724 avatar May 21 '26 01:05 z43460018724

Yes, ReActAgent is not thread safe, so it should not share across sessions.

hexly666 avatar May 21 '26 02:05 hexly666

@hexly666 Thanks for confirming!

Conclusion confirmed:

  • ReActAgent is NOT thread-safe → cannot be shared across sessions
  • Creating separate ReActAgent + Mem0LongTermMemory per (userId, sessionId) is the intended design
  • Model can be safely shared

This is working as designed. Closing this issue.

Thanks for the collaboration!

z43460018724 avatar May 21 '26 02:05 z43460018724