# Why is all messages still being passed when LLM is called? Trim\_message isn't working. Here is my code

**URL:** <https://forum.langchain.com/t/why-is-all-messages-still-being-passed-when-llm-is-called-trim-message-isnt-working-here-is-my-code/2334>\
**Category:** LangChain\
**Tags:** python-help\
**Created:** [November 25, 2025, 2:48pm UTC](https://forum.langchain.com/t/why-is-all-messages-still-being-passed-when-llm-is-called-trim-message-isnt-working-here-is-my-code/2334 "2025-11-25T14:48:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hopegood](https://yyz1.discourse-cdn.com/flex007/user_avatar/forum.langchain.com/hopegood/32/1401_2.png) [@hopegood](https://forum.langchain.com/u/hopegood)\
**Post date:** [November 25, 2025, 2:48pm UTC](https://forum.langchain.com/t/why-is-all-messages-still-being-passed-when-llm-is-called-trim-message-isnt-working-here-is-my-code/2334/1 "2025-11-25T14:48:47Z")

</div>

```auto
from langchain.messages import RemoveMessage

from langgraph.graph.message import REMOVE_ALL_MESSAGES

from langgraph.checkpoint.memory import InMemorySaver

from langchain.agents import create_agent, AgentState

from langchain.agents.middleware import before_model

from langgraph.runtime import Runtime

from langchain_core.runnables import RunnableConfig

from typing import Any

from localLLMConfiger import office_chat_model as model

import fiddlerConfiger

from langchain_core.messages.utils import trim_messages

from langchain.messages import SystemMessage,HumanMessage,AIMessage

messages = [

SystemMessage("you're a good assistant, you always respond with a joke."),

HumanMessage("i wonder why it's called langchain"),

AIMessage(

            'Well, I guess they thought "WordRope" and "SentenceString" just '

            "didn't have the same ring to it!"

        ),

HumanMessage("and who is harrison chasing anyways"),

AIMessage(

            "Hmmm let me think.\\n\\nWhy, he's probably chasing after the last "

            "cup of coffee in the office!"

        ),

HumanMessage("what's my name")\]

print(trim_messages(messages,

                max_tokens=3,

                token_counter=len,

                strategy="last",

                start_on="human",

                include_system=True))

@before_model

def __trim_messages(state: AgentState, runtime: Runtime) → dict[str, Any] | None:

"""Keep only the last few messages to fit context window."""

messages = state\["messages"\]

msg = trim_messages(messages,

                max_tokens=3,

                token_counter=len,

                strategy="last",

                start_on="human",

                include_system=True

    )

return {

    "messages": msg

    

}

agent = create_agent(

model,

middleware=\[\__trim_messages\],

checkpointer=InMemorySaver(),

)

config: RunnableConfig = {“configurable”: {“thread_id”: “1765”}}

final_response = agent.invoke({“messages”: messages}, config)

final_response[“messages”][-1].pretty_print()

```

---

<div class="post-metadata">

**Author:** ![pawel-twardziak](https://yyz1.discourse-cdn.com/flex007/user_avatar/forum.langchain.com/pawel-twardziak/32/960_2.png) [@pawel-twardziak](https://forum.langchain.com/u/pawel-twardziak)\
**Post date:** [November 25, 2025, 3:38pm UTC](https://forum.langchain.com/t/why-is-all-messages-still-being-passed-when-llm-is-called-trim-message-isnt-working-here-is-my-code/2334/2 "2025-11-25T15:38:34Z")

</div>

Hi @hopegood

could you format you post again please? Right now I’m having a hard time trying to read and understand it 😢

And it would be helpful if you shared more code on how the graph is built 🙂

---

<div class="post-metadata">

**Author:** ![hopegood](https://yyz1.discourse-cdn.com/flex007/user_avatar/forum.langchain.com/hopegood/32/1401_2.png) [@hopegood](https://forum.langchain.com/u/hopegood)\
**Post date:** [November 26, 2025, 1:26am UTC](https://forum.langchain.com/t/why-is-all-messages-still-being-passed-when-llm-is-called-trim-message-isnt-working-here-is-my-code/2334/3 "2025-11-26T01:26:02Z")

</div>

Sorry, the formatting was incorrect when I copied the code. I have now resubmitted the code.

---

<div class="post-metadata">

**Author:** ![hopegood](https://yyz1.discourse-cdn.com/flex007/user_avatar/forum.langchain.com/hopegood/32/1401_2.png) [@hopegood](https://forum.langchain.com/u/hopegood)\
**Post date:** [November 26, 2025, 9:44am UTC](https://forum.langchain.com/t/why-is-all-messages-still-being-passed-when-llm-is-called-trim-message-isnt-working-here-is-my-code/2334/4 "2025-11-26T09:44:01Z")

</div>

In `@wrap_model_call`, use the following code:  
request.messages = trim\_messages(  
request.messages,  
max\_tokens=3,  
token\_counter=len,  
strategy=“last”,  
start\_on=“human”,  
include\_system=True  
)  
return handler(request)`
