Skip to content

Add tool-call logging with verbose mode#10

Merged
pnilan merged 3 commits intomainfrom
feat/tool-call-logging
Mar 13, 2026
Merged

Add tool-call logging with verbose mode#10
pnilan merged 3 commits intomainfrom
feat/tool-call-logging

Conversation

@pnilan
Copy link
Owner

@pnilan pnilan commented Mar 13, 2026

What

Adds structured logging for every PydanticAI tool invocation, giving operators
visibility into which tools the agent calls during a conversation. A new
-v/--verbose CLI flag enables debug-level output that includes tool arguments.

How

  • patchwork/logging_config.py — New module with setup_logging(verbose) that
    configures a patchwork logger writing to stderr. Idempotent (safe to call
    multiple times without adding duplicate handlers).

  • patchwork/cli.py_make_event_handler() returns an async callback matching
    PydanticAI's EventStreamHandler signature. It filters for FunctionToolCallEvent,
    logs the tool name at INFO, optionally logs args at DEBUG in verbose mode, and prints
    a dim Rich indicator (⚙ tool_name) to the console. The handler is wired into
    agent.run_stream() via the event_stream_handler parameter. An argparse parser
    adds -v/--verbose, and errors now go through logger.exception().

  • Teststest_logging_config.py (6 tests) covers logger creation, handler
    levels, and idempotency. test_tool_logging.py (4 tests) covers tool name logging,
    verbose args, non-tool event filtering, and multiple events.

Recommended Review Order

  1. patchwork/logging_config.py — small standalone module
  2. patchwork/cli.py — event handler + CLI wiring
  3. tests/test_logging_config.py
  4. tests/test_tool_logging.py

pnilan added 2 commits March 13, 2026 10:59
Log each PydanticAI tool invocation via Python logging to stderr and
print a dim Rich indicator (⚙ tool_name) in the REPL. A new -v/--verbose
flag includes tool arguments in debug-level output. Logging setup is
idempotent and isolated in patchwork/logging_config.py.
@pnilan
Copy link
Owner Author

pnilan commented Mar 13, 2026

Two issues stood out in this patch:

  1. --verbose is not actually the gate for user-visible tool logging. In patchwork/cli.py, console.print(f"[dim]⚙ {tool_name}[/dim]") runs for every FunctionToolCallEvent, even when verbose=False. That changes the default CLI transcript for all users, not just users who opted into verbose mode, and it will interleave extra lines into any consumer expecting only the assistant stream. If the goal is to show tool activity only in verbose mode, I'd gate that print behind verbose or introduce a separate explicit flag like --show-tools.

  2. setup_logging() can't honor a later verbose=True call once the logger has already been initialized. patchwork/logging_config.py returns early whenever handlers already exist, so the handler level from the first call is frozen. A repro is setup_logging(False) followed by setup_logging(True): debug tool-arg logs remain disabled even though the second call asked for verbose mode. I'd keep the idempotent single-handler behavior, but always update the handler level/formatter on subsequent calls instead of returning immediately.

I'd also add one regression test for each behavior:

  • a test proving non-verbose mode does not print tool indicators
  • a test proving setup_logging(False) followed by setup_logging(True) enables debug output on the same logger

If the always-on tool indicator is intentional, I think that should be called out explicitly in the PR description because it's a UX change, not just logging infrastructure.

- Move console.print of ⚙ tool_name inside the verbose branch so
  default mode only logs to stderr without changing REPL output
- Update setup_logging to always apply the handler level on subsequent
  calls instead of returning early, so verbose=True is honored even
  if the logger was previously initialized with verbose=False
- Add regression tests for both behaviors
@pnilan pnilan merged commit a847090 into main Mar 13, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant