Skip to main content

StatefulToolEnv

Environment for tasks where tools require per-rollout state (e.g., sandbox IDs, database connections, sessions).

Overview

StatefulToolEnv extends ToolEnv to support tools that need persistent state across multiple calls within a rollout:
  • Hidden arguments: Some tool arguments can be hidden from the model and injected automatically
  • State injection: Use update_tool_args() to inject state into tool calls
  • Per-rollout isolation: Each rollout maintains its own state

Inheritance

Constructor

Same parameters as ToolEnv.
Do NOT pass tools in the constructor. Use add_tool() instead to specify hidden arguments.

Core Methods

update_tool_args

Abstract method - Must be implemented by subclasses. Update tool arguments and/or state before calling the tool. Use this to inject hidden arguments.
str
Name of the tool being called.
dict
Arguments from the model’s tool call.
vf.Messages
Conversation history.
vf.State
Current rollout state (can be modified in-place).
Returns: dict - Updated tool arguments (including hidden args).

add_tool

Add a tool with optionally hidden arguments.
Callable
Python function to add as a tool.
list[str]
default:"[]"
List of argument names to hide from the model’s view. These arguments will be removed from the tool schema but can be injected via update_tool_args().

remove_tool

Remove a tool from the environment.

env_response

Process tool calls, calling update_tool_args() before each tool execution. Implemented by StatefulToolEnv - do not override.

Example Usage

Sandbox Environment

Database Session

API Session with Authentication

Multi-Argument State Injection

Conditional State Injection

Schema Filtering

Hidden arguments are removed from the tool schema shown to the model:

Common Patterns

Setup and Cleanup

Use setup_state() and @vf.cleanup for resource management:

Per-Tool State Injection

Inject different state for different tools:

Dynamic State Updates

Modify state based on tool calls:

When to Use

Use StatefulToolEnv for:
  • Sandbox environments (code execution, file operations)
  • Database transactions
  • Authenticated API sessions
  • Stateful workflows
  • Tools requiring persistent connections
Use ToolEnv for:
  • Stateless, idempotent tools
  • Read-only operations
  • Tools with no shared state

See Also