How to manually add messages to history messages?

I want to manually add messages to history messages for next query.

For example ,add a chat message in memory.

{
"user": "remember your name is Bob, now",
"assistant": "Yes, I am Bob"
}

How do I write code?

Hi @huangsh
Thanks for reaching out and for using Agno! I’ve looped in the right engineers to help with your question. We usually respond within 48 hours, but if this is urgent, just let us know, and we’ll do our best to prioritize it.
Appreciate your patience—we’ll get back to you soon! :smile:

I am gradually studying the functionality of Agno, and I have the same question. I want to use Agno for a bot in Discord, and I need the ability to add messages from channel users to the memory context, even if they were not directed at the bot.

At first, I tried adding missed messages through ‘messages’ during agent.run, but there is some specificity in the order of adding messages to the context.

I’ve settled on using this function for now. But I am curious about how the developers will respond regarding the correct way to add messages to the context without triggering the LLM.

def add_message_to_memory(agent: Agent, content: str, role: str = "user") -> None:
    pseudo_run = AgentRun(
        response=RunResponse(
            run_id=str(uuid.uuid4()),
            session_id=agent.session_id,
            agent_id = agent.agent_id if agent.agent_id else str(uuid.uuid4()),
            messages=[Message(role=role, content=content)]
        )
    )
    agent.memory.runs.append(pseudo_run)

Hi
These are great requests. We are looking to redo memory and this is an important feature.
So we always have a built-in agent memory and we add messages to this memory after each “run” using memory.add_messages(...). So whether you assign your own AgentMemory or not, you could access this.
You could do agent.memory.add_messages(...) which appends to the list of messages, but you are correct in that this has a very particular order which we manage internally. So it has to be system message, then user message, then the back and forth with the model.

So @huangsh, my assumption is you want to add those messages to the memory “after” a run? Then you can just use the above method.

@Alex88 I think that method would work for you as well?

Hi,
@Alex88 @Dirk Thank you very much for your advices.

I want to manually add messages to history messages, and hope the agent will remember or refer the message I added in next running.

It would yes, if you use that method it should work. This is a little outside of the intended use, and we will definitely design the next iteration to take this into account so it is easier in future.

In version 1.1.13, ‘agent.memory’ had a function ‘add_message’ (now only ‘add_messages’ remains). I tried to use it, and the messages seem to be added to memory, as they are displayed through
pprint([m.model_dump(include={"role", "content"}) for m in agent.memory.messages]),
but they do not reach the LLM.

Am I doing something wrong? I tried again using ‘add_messages’, and the result is the same.

memory = AgentMemory()

agent = Agent(model=Gemini(id="gemini-2.0-flash-exp"),
	show_tool_calls=True,
	memory=memory,
	add_history_to_messages=True,
	num_history_responses=30,
	session_id="13456",
	debug_mode=True
)

def add_message_to_memory(memory: AgentMemory, content: str, role: str = "user") -> None:
    new_message = Message(role=role, content=content)
    memory.add_messages([new_message])

add_message_to_memory(agent.memory, "Hello, my name is Alex")

run_response: Iterator[RunResponse] = agent.run(
  "Hey, do you know my name?",
  stream=True,
)
for chunk in run_response:
    print(chunk.content)

pprint([m.model_dump(include={"role", "content"}) for m in agent.memory.messages])

Hi @Alex88
There are multiple approaches here. We are working on revamping memory so that you could do exactly what you are trying to do. Unfortunately the “message history” that the model gets is from previous runs, so you could:

  1. Create a run with that message (this is not perfect, because this unnecessarily invokes the model). This would work for sure.
  2. You could manually create a run by using memory.add_run(). This would be more involved because you have to “fake” quite a few things.
  3. You could manually add a memory. You could run memory.update_memory("The user's name is Alex"). This is also not convenient because then you have to have an explicit memory DB.

Example

from pprint import pprint
from typing import Iterator

from agno.agent import Agent
from agno.memory import AgentMemory
from agno.memory.db.sqlite import SqliteMemoryDb
from agno.models.google import Gemini
from agno.run.response import RunResponse

memory = AgentMemory(create_user_memories=True, db=SqliteMemoryDb(
    table_name="memories",
    db_file="tmp/agent_memory.db",
))

agent = Agent(model=Gemini(id="gemini-2.0-flash-exp"),
	show_tool_calls=True,
	memory=memory,
	add_history_to_messages=True,
	num_history_responses=30,
	session_id="13456",
	debug_mode=True
)

print(agent.memory.update_memory("Hello, my name is Alex"))

run_response: Iterator[RunResponse] = agent.run(
  "Hey, do you know my name?",
  stream=True,
)
for chunk in run_response:
    print(chunk.content)

pprint([m.model_dump(include={"role", "content"}) for m in agent.memory.messages])

We are looking into this in general and want to create a much better experience going forward.