adk-python icon indicating copy to clipboard operation
adk-python copied to clipboard

Ways to reduce context or event history sent to orchestrator llm

Open kanlanc opened this issue 1 year ago • 10 comments

My agents keep crashing with the error code 500 and refer me to the generative ai documentation to read on.

It's most likely too much context error and I would like to know if there is a way to reduce context or summarize context whenever there is a point an LLM's context is bloated.

kanlanc avatar May 29 '25 22:05 kanlanc

I have a very similar problem I know i we use the same session there's a chain of events within the session, and its somehow important to give some context to agent, but how can we limit this, I have over 200 events and the input context blows to over 30000 input tokens and hence inducing latency. Any suggestions?

Raun96 avatar May 30 '25 07:05 Raun96

Could you apply a before_model_callback to reduce the size of LLM request?

DeanChensj avatar Jun 01 '25 20:06 DeanChensj

I was thinking of this very thing, I need to test it out

kanlanc avatar Jun 01 '25 20:06 kanlanc

From another thread, I heard about invlude_contents ='none' on the sub agent but I have yet to verify if it works

kanlanc avatar Jun 01 '25 20:06 kanlanc

https://github.com/google/adk-python/blob/3fa2ea7cb923c9f8606d98b45a23bd58a7027436/src/google/adk/flows/llm_flows/contents.py#L255

You can see this function and try to create a new one that can take some windowed history

For now I think we can either take whole content or just the most recent one as per above function definition. To answer without history we need to pass the include_content as flag string 'none' as follows

agent = LlmAgent( name="Agent", model=GEMINI_2_FLASH, instruction=SYSTEM_PROMPT, include_content="none", output_key="final_answer" )

sourabh-patil-infocusp avatar Jul 04 '25 09:07 sourabh-patil-infocusp

Hello @sourabh-patil-infocusp

Did the include_content="none" solved your problem? I'm thinking of finding a way to implement "context pruning" or "context offloading" I have the examples from langchain and I'm studying if it could reduce the tokens consumption.

Thanks!

a-ml avatar Jul 31 '25 16:07 a-ml

@a-ml Yes. If we set include_content = "none" then LLM gets only the recent question as history. But there should be an option that can do windowed approach. I did it on my own so that LLM can look at most recent N responses and queries. Surely, we can take inspiration from langchain.

sourabh-patil-infocusp avatar Aug 04 '25 06:08 sourabh-patil-infocusp

Just being able to work with the latest context with include_content = "none" is not sufficient. This is a definite issue when you have nested workflows.

However, one can handle the scenario by mutating the request passed in callback.

spsahu-dev avatar Sep 01 '25 07:09 spsahu-dev

It seems we are looking for a short-term equivalent of https://google.github.io/adk-docs/sessions/memory/, to intelligently checkpoint/summarize the most relevant chat context from the current conversation. It is not clear from https://google.github.io/adk-docs/sessions/session/ whether any of the session services provide that capability. @hangfei do we have that in the ADK roadmap?

shobsi avatar Sep 02 '25 18:09 shobsi

It's supported as filtering plugin: https://github.com/google/adk-python/blob/main/src/google/adk/plugins/context_filter_plugin.py

hangfei avatar Oct 16 '25 23:10 hangfei