Index
agent_cover.instrumentation.prompts
Prompt Instrumentation.
This module intercepts the initialization and formatting of Prompt Templates.
🔗 Architectural Relationships
Prompts are unique because they are often defined globally but used locally. This module links these two states.
- Populates: AgentRegistry (Both
definitionsandexecutions). - Depends on: AgentContextManager (Only tracks formatting if inside an agent loop).
- Complementary to: Raw Strings Scanner (This module handles Objects; the scanner handles Strings).
⚙️ How it works
- Init Strategy: Patches
__init__. Calculates a hash of the template text to create a stable ID, linking runtime objects to static files. - Execution Strategy: Patches
format. Registers usage when the prompt is filled with variables.
Usage
Classes
PromptInstrumentor
Bases: BaseInstrumentor
Instruments prompt classes to track their definition and runtime usage.
This instrumentor applies a dual-strategy approach:
1. Init Strategy: Patches __init__ to register the prompt instance in the registry immediately upon creation. This establishes the "Total Prompts" count.
2. Execution Strategy: Patches methods like format or format_messages to track when a prompt is actually used by the agent.
Attributes:
| Name | Type | Description |
|---|---|---|
registry |
AgentRegistry
|
The registry to store coverage data. |
init_strategy |
PromptInitStrategy
|
Strategy for wrapping initialization. |
exec_strategy |
PromptExecutionStrategy
|
Strategy for wrapping execution methods. |
Examples:
How it tracks a LangChain prompt:
# 1. __init__ triggers registration (Definition Coverage)
prompt = PromptTemplate.from_template("Hello {name}")
# 2. format() triggers execution (Runtime Coverage)
prompt.format(name="World")
Source code in src/agent_cover/instrumentation/prompts/patcher.py
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 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 | |
Functions
__init__(registry=None, context_manager=None, patch_manager=None, module_iterator=None, importer_func=None, targets_provider=None, init_strategy=None, exec_strategy=None, stack_walker=None)
Initializes the PromptInstrumentor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
registry
|
Optional[AgentRegistry]
|
The AgentRegistry. |
None
|
context_manager
|
Optional[AgentContextManager]
|
The AgentContextManager. |
None
|
patch_manager
|
Optional[PatchManager]
|
The PatchManager. |
None
|
module_iterator
|
Optional[Callable[[], Dict[str, Any]]]
|
A callable to iterate through modules. |
None
|
importer_func
|
Optional[Callable[[str], Any]]
|
A function to import modules. |
None
|
targets_provider
|
Optional[Callable[[], TargetList]]
|
A callable to provide instrumentation targets. |
None
|
init_strategy
|
Optional[PromptInitStrategy]
|
The strategy for wrapping the init method. |
None
|
exec_strategy
|
Optional[PromptExecutionStrategy]
|
The strategy for wrapping execution methods. |
None
|
stack_walker
|
Optional[Callable[[Any], Iterator[Any]]]
|
A callable used to walk the stack and |
None
|
Source code in src/agent_cover/instrumentation/prompts/patcher.py
instrument()
Instruments the prompts.
This method retrieves the targets, iterates through the modules, and applies the appropriate strategies to patch the prompt classes.
Source code in src/agent_cover/instrumentation/prompts/patcher.py
Functions
instrument_prompts(registry=None)
Backward compatibility function for instrumenting prompts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
registry
|
The AgentRegistry. |
None
|
Returns:
| Type | Description |
|---|---|
|
The PromptInstrumentor instance. |
Source code in src/agent_cover/instrumentation/prompts/patcher.py
register_existing_prompts(registry=None, root_path=None, module_iterator=None)
Registers existing prompts by scanning modules.
This function scans modules for prompts that are already defined.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
registry
|
Optional[AgentRegistry]
|
The AgentRegistry to register the prompts with. |
None
|
root_path
|
Optional[str]
|
The root path to search for modules. |
None
|
module_iterator
|
Optional[Callable]
|
A callable to iterate through modules. |
None
|