state

A google-adk object that stores information for an agent conversation and records changes made during one turn or callback. It supports information scoped to a session, a user, or a single temporary operation.

In plain words
What is it for?
Use it to remember conversation details, store user preferences, keep temporary tool results, and identify what changed during an agent step.
Why use it?
It provides a controlled way to read, save, and track context instead of passing an ordinary dictionary through every part of an agent workflow.

Cursor rule

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add rules/altaidevorg/rules-for-ai/state
Clone the repo
git clone --depth 1 https://github.com/altaidevorg/rules-for-ai
Per session 0 Nothing until a file matches its globs; then the whole rule loads.
When invoked 3,324 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00000 $0.03324
Opus 5 $0.00000 $0.01662
Sonnet 5 $0.00000 $0.00665
Haiku 4.5 $0.00000 $0.00332

Measured yesterday against content hash c905a8b13976, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

state scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured yesterday.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

examples/google-adk/state.mdc · 237 lines

How it starts

The opening of the file, as written. The whole thing — 237 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Chapter 4: State

In the previous chapter, we saw how the Session object holds conversational history (events) and a state dictionary to maintain context across turns. This chapter dives deep into that state dictionary's wrapper: the State object. It's designed to provide a consistent view of the session's state while meticulously tracking changes made during a specific agent turn or callback execution.

Motivation and Use Case

When an agent processes a request, it often needs to:

  1. Read existing information: What did the user previously specify as their destination?
  2. Store new information: The agent just identified the user's budget.
  3. Track intermediate changes: A tool call successfully fetched flight options, which need to be stored temporarily before being presented to the user or used in the next LLM call.
  4. Manage different scopes: Some information might be relevant only to the current chat (session state), while other details (like user preferences) should persist across different chats (user state).

Managing this directly within a simple dictionary passed around can be error-prone. How do we ensure that changes made during a complex turn (involving callbacks, LLM calls, tool executions) are correctly captured and associated with the final outcome (Event) for persistence?

The State object addresses this by acting as a smart dictionary wrapper. It provides access to the underlying session state but records any modifications made during the current context (e.g., a callback or tool execution) into a separate "delta". This delta is then attached to the resulting Event, ensuring the Session (Session / BaseSessionService) can accurately update the persistent state.

Central Use Case: Consider our travel agent bot.

  • The Session starts with state = {'origin': 'London'}.
  • The agent's run_async is called. An InvocationContext is created.
  • Inside a before_model_callback, we access ctx.state. ctx.state['origin'] returns 'London'.
  • The callback sets a temporary flag: ctx.state[State.TEMP_PREFIX + 'needs_clarification'] = True. This is added to the delta but won't be persisted.
  • The LLM runs and invokes a find_flights tool.
  • Inside the tool's execution, it accesses ctx.state via ToolContext. It finds flights and updates the state: ctx.state['available_flights'] = [...]. This change is recorded in the delta. ctx.state['origin'] still returns 'London'.
  • The tool yields an Event. This event's actions.state_delta now contains {'available_flights': [...]}. The temp: key is not included for persistence.
  • The Runner calls session_service.append_event with this event. The service updates the persistent session state by merging the state_delta.
  • In the next turn, when the Session is loaded, its base state will include {'origin': 'London', 'available_flights': [...]}.

Read the full file on GitHub · 237 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. yesterday First seen · 237 lines · 0 tokens per session scan A c905a8b13976

Subscribe to this mod's changes

state is a cursor rule published in the GitHub repository altaidevorg/rules-for-ai (2 stars, last pushed 1y ago), licensed MIT. It costs nothing until one of its globs matches a file; then it loads 3,324 tokens. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-31.