Repository navigation
feat: move dynamic context (datetime/location) from system message to trailing message - #9544
SiAgentBuild wants to merge 2 commits into
Conversation
|
| Filename | Overview |
|---|---|
| libs/agno/agno/agent/_messages.py | Relocates Agent dynamic context into a second persisted system message, exposing adapter overwrite and stale-history behavior. |
| libs/agno/agno/team/_messages.py | Mirrors the Agent implementation and therefore has the same provider compatibility and persistence defects. |
| libs/agno/tests/unit/agent/test_datetime_format.py | Updates helper-level datetime tests but does not exercise full message serialization or multi-turn persistence. |
| libs/agno/tests/unit/team/test_datetime_format.py | Updates Team helper tests but does not cover adapter handling or persisted history. |
Sequence Diagram
sequenceDiagram
participant Run as Agent/Team run
participant History as Session history
participant Builder as Dynamic context builder
participant Adapter as Model adapter
Run->>History: Load prior messages
History-->>Run: System prompt and conversation history
Run->>Builder: Generate current datetime/location
Builder-->>Run: Second system-role message
Run->>Adapter: Send ordered message list
Note over Adapter: Last-system-only adapters replace the primary prompt
Adapter-->>Run: Model response
Run->>History: "Persist messages with add_to_agent_memory=True"
Note over History: Dynamic context is retained for later turns
Reviews (1): Last reviewed commit: "feat: move dynamic context (datetime/loc..." | Re-trigger Greptile
| if not parts: | ||
| return None | ||
|
|
||
| return Message(role="system", content="\n".join(parts)) |
There was a problem hiding this comment.
Trailing system message replaces prompt
When datetime or location context is enabled with Google Gemini, Gemini Interactions, or AWS Bedrock, this second system message becomes the adapter's retained system value, replacing the Agent or Team's primary prompt and causing the model to lose its configured instructions and behavioral guidance.
| if not parts: | ||
| return None | ||
|
|
||
| return Message(role="system", content="\n".join(parts)) |
There was a problem hiding this comment.
Dynamic context persists into history
When a persisted Agent or Team session runs multiple turns with dynamic context enabled, this message retains the default add_to_agent_memory=True, so stale datetime or location statements are loaded as history alongside the newly generated values and are exposed through session-message consumers.
Address review feedback on agno-agi#9544: 1. Change role from system to user — adapters like Gemini and Bedrock extract the last system message as the primary prompt, which would overwrite the agent's configured instructions. Using role=user keeps the dynamic context in the message flow without affecting the system prompt. 2. Set add_to_agent_memory=False — prevents stale per-turn datetime and location values from being persisted and loaded as history in subsequent runs.
|
Both issues addressed in ce31d45:
Tests updated accordingly. All 1061 agent/team unit tests pass, format/validate clean. |
… trailing message Move add_datetime_to_context and add_location_to_context output from the system message to a separate message appended after history and before the user message. This fixes two issues: 1. Dynamic context was silently dropped when a custom system_message was set, because get_system_message() returns early on that branch. 2. Dynamic values embedded in the system message broke prompt caching on every request, since cache matching works from the first token forward. Applied to both Agent and Team, sync and async paths. Closes agno-agi#9543
Address review feedback on agno-agi#9544: 1. Change role from system to user — adapters like Gemini and Bedrock extract the last system message as the primary prompt, which would overwrite the agent's configured instructions. Using role=user keeps the dynamic context in the message flow without affecting the system prompt. 2. Set add_to_agent_memory=False — prevents stale per-turn datetime and location values from being persisted and loaded as history in subsequent runs.
32d8ee9 to
c06358a
Compare
|
Hi @kausmeows @ysolanky — rebased onto v3.0.0 and resolved the conflict in Also verified on v3 that Agent/team unit suites pass (1453 tests). Ready for review, thanks! |
Summary
Move
add_datetime_to_contextandadd_location_to_contextoutput from the system message to a separate trailing message appended after history and before the user message. This fixes two issues:Dynamic context was silently dropped when a custom
system_messagewas set.get_system_message()returns early on the customsystem_messagebranch, skipping the entire<additional_information>block. Users who set bothsystem_messageandadd_datetime_to_context=Truegot no time info and no warning.Dynamic values in the system message broke prompt caching. Prompt caching (Anthropic, OpenAI) matches tokens from the first position forward. Embedding
The current time is 2026-08-13 14:22:47.774825+08:00(microsecond precision by default) in the system message guaranteed a full cache miss on every request, causing the entire system prompt to be re-processed at full cost.By appending the dynamic context as a separate message near the end of the messages list, the system message and history stay byte-identical across requests, preserving prompt cache hits. The dynamic context also works identically whether or not a custom
system_messageis set.Applied to both Agent and Team, sync and async paths.
Closes #9543
Type of change
Checklist
./scripts/format.shand./scripts/validate.sh)Duplicate and AI-Generated PR Check
Additional Notes
What changed:
agno/agent/_messages.py: Removed datetime/location fromget_system_message()'sadditional_information. Added_get_dynamic_context_message()that builds a standaloneMessage(role="system", ...). Injected it inget_run_messages()between history and user message (both sync and async paths).agno/team/_messages.py: Same changes for Team.tests/unit/agent/test_datetime_format.py+tests/unit/team/test_datetime_format.py: Updated tests to target_get_dynamic_context_message()instead ofget_system_message(). Addedtest_datetime_not_in_system_messageandtest_timezone_identifier.What did NOT change:
add_datetime_to_context,add_location_to_context,timezone_identifier,datetime_format) — same API, same defaults.The current time is ...,Your approximate location is ...) — unchanged.add_name_to_context— remains in the system message, as the agent name is persona-level info that doesn't change per turn.