Memory is the memory management system in the tRPC-Agent-Go framework. It
provides persistent memory and context management for Agents. By integrating
the memory service, session management, and memory tools, the Memory system
helps Agents remember user information, maintain conversation context, and
offer personalized responses across multi-turn dialogs.
⚠️ Breaking Changes Notice
Important: The memory integration approach has been updated to provide better separation of concerns and explicit control. This is a breaking change that affects how memory services are integrated with Agents.
packagemainimport("context""log"// Core components."trpc.group/trpc-go/trpc-agent-go/agent/llmagent"memoryinmemory"trpc.group/trpc-go/trpc-agent-go/memory/inmemory""trpc.group/trpc-go/trpc-agent-go/model""trpc.group/trpc-go/trpc-agent-go/model/openai""trpc.group/trpc-go/trpc-agent-go/runner""trpc.group/trpc-go/trpc-agent-go/session/inmemory")funcmain(){ctx:=context.Background()// 1. Create the memory service.memoryService:=memoryinmemory.NewMemoryService()// 2. Create the LLM model.modelInstance:=openai.New("deepseek-chat")// 3. Create the Agent and register memory tools.llmAgent:=llmagent.New("memory-assistant",llmagent.WithModel(modelInstance),llmagent.WithDescription("An assistant with memory capabilities."),llmagent.WithInstruction("Remember important user info and recall it when needed.",),llmagent.WithTools(memoryService.Tools()),// Register memory tools.)// 4. Create the Runner with memory service.sessionService:=inmemory.NewSessionService()appRunner:=runner.NewRunner("memory-chat",llmAgent,runner.WithSessionService(sessionService),runner.WithMemoryService(memoryService),// Set memory service.)// 5. Run a dialog (the Agent uses memory tools automatically).log.Println("🧠 Starting memory-enabled chat...")message:=model.NewUserMessage("Hi, my name is John, and I like programming",)eventChan,err:=appRunner.Run(ctx,"user123","session456",message)iferr!=nil{log.Fatalf("Failed to run agent: %v",err)}// 6. Handle responses ..._=eventChan}
Core Concepts
The memory module
is the core of tRPC-Agent-Go's memory management. It provides complete memory
storage and retrieval capabilities with a modular design that supports
multiple storage backends and memory tools.
import("trpc.group/trpc-go/trpc-agent-go/agent/llmagent""trpc.group/trpc-go/trpc-agent-go/memory"memoryinmemory"trpc.group/trpc-go/trpc-agent-go/memory/inmemory""trpc.group/trpc-go/trpc-agent-go/runner")// Create the memory service.memoryService:=memoryinmemory.NewMemoryService()// Create the Agent and register memory tools.llmAgent:=llmagent.New("memory-assistant",llmagent.WithModel(modelInstance),llmagent.WithDescription("An assistant with memory capabilities."),llmagent.WithInstruction("Remember important user info and recall it when needed.",),llmagent.WithTools(memoryService.Tools()),// Register memory tools.)// Create the runner with memory service.appRunner:=runner.NewRunner("memory-chat",llmAgent,runner.WithMemoryService(memoryService),// Set memory service.)
Memory Service
Configure the memory service in code. Two backends are supported: in-memory
and Redis.
import(memoryinmemory"trpc.group/trpc-go/trpc-agent-go/memory/inmemory"memoryredis"trpc.group/trpc-go/trpc-agent-go/memory/redis")// In-memory implementation for development and testing.memService:=memoryinmemory.NewMemoryService()// Redis implementation for production.redisService,err:=memoryredis.NewService(memoryredis.WithRedisClientURL("redis://localhost:6379"),memoryredis.WithToolEnabled(memory.DeleteToolName,true),// Enable delete.)iferr!=nil{// Handle error.}// Register memory tools with the Agent.llmAgent:=llmagent.New("memory-assistant",llmagent.WithTools(memService.Tools()),// Or use redisService.Tools().)// Set memory service in the Runner.runner:=runner.NewRunner("app",llmAgent,runner.WithMemoryService(memService),// Or use redisService.)
Memory Tool Configuration
By default, the following tools are enabled. Others can be toggled via
configuration.
Memory IDs are generated from content + topics. Adding the same content and topics
is idempotent and overwrites the existing entry (not append). UpdatedAt is refreshed.
If you need append semantics or different duplicate-handling strategies, you can
implement custom tools or extend the service with policy options (e.g. allow/overwrite/ignore).
Custom Tool Implementation
You can override default tools with custom implementations. See
memory/tool/tool.go for reference on how to implement custom tools.
import("context""fmt""trpc.group/trpc-go/trpc-agent-go/memory"memoryinmemory"trpc.group/trpc-go/trpc-agent-go/memory/inmemory"toolmemory"trpc.group/trpc-go/trpc-agent-go/memory/tool""trpc.group/trpc-go/trpc-agent-go/tool""trpc.group/trpc-go/trpc-agent-go/tool/function")// A custom clear tool with real logic using the invocation context.funccustomClearMemoryTool()tool.Tool{clearFunc:=func(ctxcontext.Context,_*toolmemory.ClearMemoryRequest)(*toolmemory.ClearMemoryResponse,error){// Get memory service and user info from invocation context.memSvc,err:=toolmemory.GetMemoryServiceFromContext(ctx)iferr!=nil{returnnil,fmt.Errorf("custom clear tool: %w",err)}appName,userID,err:=toolmemory.GetAppAndUserFromContext(ctx)iferr!=nil{returnnil,fmt.Errorf("custom clear tool: %w",err)}iferr:=memSvc.ClearMemories(ctx,memory.UserKey{AppName:appName,UserID:userID});err!=nil{returnnil,fmt.Errorf("custom clear tool: failed to clear memories: %w",err)}return&toolmemory.ClearMemoryResponse{Message:"🎉 All memories cleared successfully!"},nil}returnfunction.NewFunctionTool(clearFunc,function.WithName(memory.ClearToolName),function.WithDescription("Clear all memories for the user."),)}// Register the custom tool with an InMemory service.memoryService:=memoryinmemory.NewMemoryService(memoryinmemory.WithCustomTool(memory.ClearToolName,customClearMemoryTool),)
Full Example
Below is a complete example showing how to create an Agent with memory
capabilities.
packagemainimport("context""flag""log""os""trpc.group/trpc-go/trpc-agent-go/agent/llmagent""trpc.group/trpc-go/trpc-agent-go/memory"memoryinmemory"trpc.group/trpc-go/trpc-agent-go/memory/inmemory"memoryredis"trpc.group/trpc-go/trpc-agent-go/memory/redis""trpc.group/trpc-go/trpc-agent-go/model""trpc.group/trpc-go/trpc-agent-go/model/openai""trpc.group/trpc-go/trpc-agent-go/runner""trpc.group/trpc-go/trpc-agent-go/session/inmemory")funcmain(){var(memServiceName=flag.String("memory","inmemory","Memory service type (inmemory, redis)",)redisAddr=flag.String("redis-addr","localhost:6379","Redis server address",)modelName=flag.String("model","deepseek-chat","Model name"))flag.Parse()ctx:=context.Background()// 1. Create the memory service (based on flags).varmemoryServicememory.Servicevarerrerrorswitch*memServiceName{case"redis":redisURL:=fmt.Sprintf("redis://%s",*redisAddr)memoryService,err=memoryredis.NewService(memoryredis.WithRedisClientURL(redisURL),memoryredis.WithToolEnabled(memory.DeleteToolName,true),memoryredis.WithCustomTool(memory.ClearToolName,customClearMemoryTool,),)iferr!=nil{log.Fatalf("Failed to create redis memory service: %v",err)}default:// inmemory.memoryService=memoryinmemory.NewMemoryService(memoryinmemory.WithToolEnabled(memory.DeleteToolName,true),memoryinmemory.WithCustomTool(memory.ClearToolName,customClearMemoryTool,),)}// 2. Create the LLM model.modelInstance:=openai.New(*modelName)// 3. Create the Agent and register memory tools.genConfig:=model.GenerationConfig{MaxTokens:intPtr(2000),Temperature:floatPtr(0.7),Stream:true,}llmAgent:=llmagent.New("memory-assistant",llmagent.WithModel(modelInstance),llmagent.WithDescription("An assistant with memory. I can remember key info about you "+"and recall it when needed.",),llmagent.WithGenerationConfig(genConfig),llmagent.WithTools(memoryService.Tools()),// Register memory tools.)// 4. Create the Runner with memory service.sessionService:=inmemory.NewSessionService()appRunner:=runner.NewRunner("memory-chat",llmAgent,runner.WithSessionService(sessionService),runner.WithMemoryService(memoryService),// Set memory service.)// 5. Run a dialog (the Agent uses memory tools automatically).log.Println("🧠 Starting memory-enabled chat...")message:=model.NewUserMessage("Hi, my name is John, and I like programming",)eventChan,err:=appRunner.Run(ctx,"user123","session456",message)iferr!=nil{log.Fatalf("Failed to run agent: %v",err)}// 6. Handle responses ..._=eventChan}// Custom clear tool.funccustomClearMemoryTool()tool.Tool{// ... implementation ...returnnil}// Helpers.funcintPtr(iint)*int{return&i}funcfloatPtr(ffloat64)*float64{return&f}
The environment variables are configured as follows:
# Choose components via flags when running the example.gorunmain.go-memoryinmemory
gorunmain.go-memoryredis-redis-addrlocalhost:6379
# Flags:# -memory: memory service type (inmemory, redis), default is inmemory.# -redis-addr: Redis server address, default is localhost:6379.# -model: model name, default is deepseek-chat.