# Practical and Useful Patterns with ADK
🧪 You can't hit production APIs during agent testing. ADK's Environment Simulation intercepts tool calls and replaces them with controlled responses, letting you reproduce edge cases without affecting production systems.
📌 Title: Environment Simulation
🔗 URL:
🧩 Overview
ADK's Environment Simulator intercepts tool calls during agent execution and replaces them with controlled responses — all without modifying agent code. It supports error injection, fixed responses, conditional injection, probabilistic failures, and latency simulation. It integrates as a Callback or Plugin, with a decision priority of: injection configs, then mock strategy, then real tool execution.
🛠 Usage
Integration as a Callback:
Import `LlmAgent` from `google.adk.agents`, `EnvironmentSimulationFactory` from ` and `EnvironmentSimulationConfig`, `InjectedError`, `InjectionConfig`, `ToolSimulationConfig` from the `environment_simulation_config` submodule. Create an `EnvironmentSimulationConfig` with a list of `ToolSimulationConfig` entries in `tool_simulation_configs`. Each entry specifies a `tool_name` (e.g., `"get_user_profile"`) and `injection_configs` containing `InjectionConfig` instances -- for example, an `InjectedError` with `injected_http_error_code=503` and an `error_message`. Convert the config to a callback via `EnvironmentSimulationFactory.create_callback(config)` and pass it as the `before_tool_callback` parameter of `LlmAgent`.
Fixed response injection:
Set the `injected_response` parameter of `InjectionConfig` to a dictionary (e.g., `{"status": "ok", "order_id": "ORD-9999"}`) to return a fixed response for tool calls.
Conditional injection based on arguments:
Use `match_args` in `InjectionConfig` to specify an argument pattern (e.g., `{"item_id": "ITEM-404"}`). The injection triggers only when tool call arguments match. Pair it with `injected_error` set to `InjectedError(injected_http_error_code=404, error_message="Item not found.")` to return a 404 error for that specific argument.
Probabilistic failure simulation:
Set `injection_probability` on `InjectionConfig` to a value like `0.3` for a 30% failure rate, and `random_seed` (e.g., `42`) for reproducibility. Combined with `injected_error` set to `InjectedError(injected_http_error_code=500, error_message="Internal server error.")`, this randomly injects 500 errors at the specified probability.
Latency simulation:
Set `injected_latency_seconds` on `InjectionConfig` to the desired delay (e.g., `5.0` seconds) along with an `injected_response` (e.g., `{"result": "slow but successful"}`). The response is returned after the specified delay.
Mock Strategy mode auto-generates responses from tool schemas:
Import `MockStrategy` from `environment_simulation_config` and set `mock_strategy_type` to `MockStrategy.MOCK_STRATEGY_TOOL_SPEC` on a `ToolSimulationConfig` with the target `tool_name` (e.g., `"create_order"`). This auto-generates responses based on the tool's schema definition.
🏗 Practical Patterns
**Testing Agent Resilience to API Failures**: Inject 503 or 500 errors to verify that agents handle retries and fallbacks correctly. Reproduce failure scenarios without touching production systems.
**Edge Case Testing with Conditional Injection**: Use `match_args` to trigger errors only for specific argument patterns — "non-existent product ID," "out-of-stock item." Pinpoint-test agent behavior under specific conditions.
**Realistic Testing with Probabilistic Failures**: Use `injection_probability` to simulate flaky APIs with, say, 30% failure rate. Fix `random_seed` for reproducible tests.
**Realistic Mock Generation with Environment Data**: Provide DB snapshots or other context via `environment_data` so the Mock Strategy generates more realistic responses:
Convert context data such as a DB snapshot (e.g., a dictionary of product records) to a JSON string via `json.dumps()` and pass it as the `environment_data` parameter of `EnvironmentSimulationConfig`. The Mock Strategy references this environment data to generate more realistic responses.
**Combining Injections with Mock Strategy**: Inject errors for specific conditions while using Mock Strategy for everything else. Injection configs are evaluated first; unmatched calls fall through to the mock strategy.
💡 Use Cases
🔧 Verifying agent resilience to external API failures
🎯 Reproducing edge cases under specific conditions
📉 Probabilistic simulation of flaky API interactions
⏱ Verifying timeout handling with high-latency APIs
⚠️ Caveats
- `injected_latency_seconds` has a maximum of 120 seconds.
- `InjectionConfig` accepts either an error or a response, not both simultaneously.
- `tool_name` must exactly match the registered tool name. Watch for typos.
- Mock Strategy depends on tool schemas — insufficient schemas lead to lower-quality generated responses.
✨ Environment Simulation is a powerful testing foundation that reproduces edge cases without contaminating production. Combine error injection with conditional mocks to verify agent resilience against every failure scenario!
#
ADK# #
AIAgent#