Strands Agents Typescript SDK
    Preparing search index...

    Interface AgentState

    Agent state provides key-value storage outside conversation context. State is not passed to the model during inference but is accessible by tools (via ToolContext) and application logic.

    All values are deep copied on get/set operations to prevent reference mutations. Values must be JSON serializable.

    const state = new AgentState({ userId: 'user-123' })
    state.set('sessionId', 'session-456')
    const userId = state.get('userId') // 'user-123'
    interface AgentState {
        get<TState, K extends string | number | symbol = keyof TState>(
            key: K,
        ): TState[K];
        get(key: string): JSONValue;
        set<TState, K extends string | number | symbol = keyof TState>(
            key: K,
            value: TState[K],
        ): void;
        set(key: string, value: unknown): void;
        delete<TState, K extends string | number | symbol = keyof TState>(
            key: K,
        ): void;
        delete(key: string): void;
        clear(): void;
        getAll(): Record<string, JSONValue>;
        keys(): string[];
    }
    Index

    Methods

    • Get a state value by key with optional type-safe property lookup. Returns a deep copy to prevent mutations.

      Type Parameters

      • TState

        The complete state interface type

      • K extends string | number | symbol = keyof TState

        The property key (inferred from argument)

      Parameters

      • key: K

        Key to retrieve specific value

      Returns TState[K]

      The value for the key, or undefined if key doesn't exist

      // Typed usage
      const user = state.get<AppState>('user') // { name: string; age: number } | undefined

      // Untyped usage
      const value = state.get('someKey') // JSONValue | undefined
    • Get a state value by key with optional type-safe property lookup. Returns a deep copy to prevent mutations.

      Parameters

      • key: string

        Key to retrieve specific value

      Returns JSONValue

      The value for the key, or undefined if key doesn't exist

      // Typed usage
      const user = state.get<AppState>('user') // { name: string; age: number } | undefined

      // Untyped usage
      const value = state.get('someKey') // JSONValue | undefined
    • Set a state value with optional type-safe property validation. Validates JSON serializability and stores a deep copy.

      Type Parameters

      • TState

        The complete state interface type

      • K extends string | number | symbol = keyof TState

        The property key (inferred from argument)

      Parameters

      • key: K

        The key to set

      • value: TState[K]

        The value to store (must be JSON serializable)

      Returns void

      Error if value is not JSON serializable

      // Typed usage
      state.set<AppState>('user', { name: 'Alice', age: 25 })

      // Untyped usage
      state.set('someKey', { any: 'value' })
    • Set a state value with optional type-safe property validation. Validates JSON serializability and stores a deep copy.

      Parameters

      • key: string

        The key to set

      • value: unknown

        The value to store (must be JSON serializable)

      Returns void

      Error if value is not JSON serializable

      // Typed usage
      state.set<AppState>('user', { name: 'Alice', age: 25 })

      // Untyped usage
      state.set('someKey', { any: 'value' })
    • Delete a state value by key with optional type-safe property validation.

      Type Parameters

      • TState

        The complete state interface type

      • K extends string | number | symbol = keyof TState

        The property key (inferred from argument)

      Parameters

      • key: K

        The key to delete

      Returns void

      // Typed usage
      state.delete<AppState>('user')

      // Untyped usage
      state.delete('someKey')
    • Delete a state value by key with optional type-safe property validation.

      Parameters

      • key: string

        The key to delete

      Returns void

      // Typed usage
      state.delete<AppState>('user')

      // Untyped usage
      state.delete('someKey')