Infrastructure Shields API¶
Guardrail capabilities for pydantic-ai agents.
Ready-to-use capabilities for safety, cost control, and permission management. Built on pydantic-ai's native capabilities API.
Example
CostTracking
dataclass
¶
Bases: AbstractCapability[Any]
Track token usage and API costs with optional budget enforcement.
Accumulates token usage across runs, calculates USD costs using genai-prices, and enforces optional budget limits.
Example
Source code in src/pydantic_ai_shields/guardrails.py
| Python | |
|---|---|
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 | |
model_name = None
class-attribute
instance-attribute
¶
Model name for cost lookup (e.g. "openai:gpt-4.1"). Auto-detected if None.
budget_usd = None
class-attribute
instance-attribute
¶
Maximum allowed cumulative cost. None = unlimited.
on_cost_update = None
class-attribute
instance-attribute
¶
Callback invoked after each run with CostInfo.
total_cost
property
¶
Cumulative USD cost across all runs.
total_request_tokens
property
¶
Cumulative input tokens.
total_response_tokens
property
¶
Cumulative output tokens.
run_count
property
¶
Number of completed runs.
before_run(ctx)
async
¶
Resolve prices on first run using model info from context.
Source code in src/pydantic_ai_shields/guardrails.py
after_run(ctx, *, result)
async
¶
Track usage after run completes.
Source code in src/pydantic_ai_shields/guardrails.py
CostInfo
dataclass
¶
Token usage and cost information for a run.
Attributes:
| Name | Type | Description |
|---|---|---|
run_cost_usd |
float | None
|
USD cost of this run (None if model unknown). |
total_cost_usd |
float | None
|
Cumulative USD cost across all runs (None if model unknown). |
run_request_tokens |
int
|
Input tokens for this run. |
run_response_tokens |
int
|
Output tokens for this run. |
total_request_tokens |
int
|
Cumulative input tokens across all runs. |
total_response_tokens |
int
|
Cumulative output tokens across all runs. |
run_count |
int
|
Number of completed runs so far. |
Source code in src/pydantic_ai_shields/guardrails.py
ToolGuard
dataclass
¶
Bases: AbstractCapability[Any]
Control tool access: block tools, require approval, or allow freely.
Uses prepare_tools to hide blocked tools from the model entirely,
and before_tool_execute to enforce approval for sensitive tools.
Example
from pydantic_ai import Agent
from pydantic_ai_shields import ToolGuard
async def ask_user(tool_name, args):
return input(f"Allow {tool_name}? (y/n) ") == "y"
agent = Agent(
"openai:gpt-4.1",
capabilities=[ToolGuard(
blocked=["execute"],
require_approval=["write_file", "edit_file"],
approval_callback=ask_user,
)],
)
Source code in src/pydantic_ai_shields/guardrails.py
blocked = field(default_factory=list)
class-attribute
instance-attribute
¶
Tool names to block entirely (hidden from model).
require_approval = field(default_factory=list)
class-attribute
instance-attribute
¶
Tool names that require human approval before execution.
approval_callback = None
class-attribute
instance-attribute
¶
Async callback: (tool_name, args) -> bool. Required when require_approval is set.
prepare_tools(ctx, tool_defs)
async
¶
Hide blocked tools from the model.
Source code in src/pydantic_ai_shields/guardrails.py
| Python | |
|---|---|
before_tool_execute(ctx, *, call, tool_def, args)
async
¶
Check approval for sensitive tools.
Source code in src/pydantic_ai_shields/guardrails.py
InputGuard
dataclass
¶
Bases: AbstractCapability[Any]
Block or modify user input based on a guardrail check.
The guard function receives the user prompt and returns True if safe.
Example
Source code in src/pydantic_ai_shields/guardrails.py
guard = None
class-attribute
instance-attribute
¶
Function that checks input safety. Returns True if safe.
before_run(ctx)
async
¶
Check input before run starts.
Source code in src/pydantic_ai_shields/guardrails.py
OutputGuard
dataclass
¶
Bases: AbstractCapability[Any]
Block or modify model output based on a guardrail check.
The guard function receives the model output text and returns True if safe.
Example
Source code in src/pydantic_ai_shields/guardrails.py
guard = None
class-attribute
instance-attribute
¶
Function that checks output safety. Returns True if safe.
after_run(ctx, *, result)
async
¶
Check output after run completes.
Source code in src/pydantic_ai_shields/guardrails.py
AsyncGuardrail
dataclass
¶
Bases: AbstractCapability[Any]
Run a guardrail concurrently with the LLM call.
Launches the guardrail check as a background task while the model generates a response. If the guardrail fails before the model finishes, the run is short-circuited to save API costs.
Timing modes:
- "concurrent": Guardrail runs alongside model; fail-fast on violation
- "blocking": Guardrail completes before model starts (traditional)
- "monitoring": Guardrail runs after model (fire-and-forget, non-blocking)
Example
Source code in src/pydantic_ai_shields/guardrails.py
| Python | |
|---|---|
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 | |
guard = None
class-attribute
instance-attribute
¶
The guardrail capability to run asynchronously.
timing = 'concurrent'
class-attribute
instance-attribute
¶
When to run the guardrail relative to the model call.
cancel_on_failure = True
class-attribute
instance-attribute
¶
Cancel/reject output if guardrail fails (concurrent mode only).
timeout = None
class-attribute
instance-attribute
¶
Maximum time to wait for the guardrail.
name = 'AsyncGuardrail'
class-attribute
instance-attribute
¶
Name for logging.
wrap_run(ctx, *, handler)
async
¶
Wrap the entire run to manage concurrent guardrail execution.