You are right to point out that time passed will be static and will not be updated at runtime as also pointed out by @khteh as well.
You’re already passing timestamp in your state:
{"messages": [{"role": "user", "content": message}], "timestamp": datetime.now()}
Therefore I have three more suggestion that are better suited:
Approach 1: Include timestamp in the user message itself
The simplest approach - format the timestamp directly into the message content:
timestamp = datetime.now()
formatted_ts = timestamp.strftime("%d-%m-%Y_%H-%M-%S")
user_message = f"""[Request Timestamp: {formatted_ts}]
{message}
Note: When saving files, use this exact timestamp suffix: _{formatted_ts}"""
async for step in current_app.agent.astream(
{"messages": [{"role": "user", "content": user_message}]},
stream_mode="values",
config=config,
):
result.append(step["messages"][-1])
Approach 2: Use a custom state schema with timestamp field
If you’re using LangGraph, you can define a custom state that includes the timestamp and have your nodes access it:
from typing import TypedDict, Annotated
from langgraph.graph.message import add_messages
class AgentState(TypedDict):
messages: Annotated[list, add_messages]
timestamp: str # Pre-formatted timestamp string
# When invoking:
timestamp = datetime.now().strftime("%d-%m-%Y_%H-%M-%S")
async for step in current_app.agent.astream(
{
"messages": [{"role": "user", "content": message}],
"timestamp": timestamp
},
stream_mode="values",
config=config,
):
result.append(step["messages"][-1])
Then in your agent’s system prompt or instructions, reference that the timestamp is available in the state (Just like how I referred in my earlier response but this time will be dynamic).
Approach 3: Inject timestamp via configurable fields
You can pass runtime values through the config’s configurable field:
timestamp = datetime.now().strftime("%d-%m-%Y_%H-%M-%S")
config = {
"configurable": {
"thread_id": thread_id,
"request_timestamp": timestamp
}
}
async for step in current_app.agent.astream(
{"messages": [{"role": "user", "content": message}]},
stream_mode="values",
config=config,
):
result.append(step["messages"][-1])
Then your nodes can access config["configurable"]["request_timestamp"].
Recommended Prompt Structure
WORKFLOW_INSTRUCTIONS = """You are a helpful question-answering assistant.
**Important**: Each request has a unique timestamp identifier provided at the start of the user message in the format [Request Timestamp: DD-MM-YYYY_HH-MM-SS]. You MUST use this exact timestamp for ALL file operations in this request to ensure consistency.
**File Naming Convention**:
- Save user questions to: `/user_questions_{timestamp}.md`
- Write final reports to: `/final_answer_{timestamp}.md`
The timestamp MUST remain consistent across all file operations within a single request turn."""
The key insight is that you want the timestamp to be runtime dynamic per-request but static within a single request’s execution…..
I hope this helps @feng-1985 @khteh