Skip to content

Instantly share code, notes, and snippets.

@simonw

simonw/pretty.md Secret

Last active April 18, 2025 10:35
Show Gist options
  • Save simonw/54fc42ef9a7fb8f777162bbbfbba4f23 to your computer and use it in GitHub Desktop.
Save simonw/54fc42ef9a7fb8f777162bbbfbba4f23 to your computer and use it in GitHub Desktop.

pretty-print messages

Here's a version of my one-liner that pretty prints the messages exchanged with the LLM:

ANTHROPIC_API_KEY="$(llm keys get anthropic)" \
uv run --with devtools --with pydantic-ai python -c '
import asyncio
from devtools import pprint
from pydantic_ai import Agent, capture_run_messages
from pydantic_ai.mcp import MCPServerStdio

server = MCPServerStdio(
    "deno",
    args=[
        "run",
        "-N",
        "-R=node_modules",
        "-W=node_modules",
        "--node-modules-dir=auto",
        "jsr:@pydantic/mcp-run-python",
        "stdio",
    ],
)

agent = Agent("claude-3-5-haiku-latest", mcp_servers=[server])

async def main():
    with capture_run_messages() as messages:
        async with agent.run_mcp_servers():
            result = await agent.run("How many days between 2000-01-01 and 2025-03-18?")
    pprint(messages)
    print(result.output)

asyncio.run(main())'

Output

[
    ModelRequest(
        parts=[
            UserPromptPart(
                content='How many days between 2000-01-01 and 2025-03-18?',
                timestamp=datetime.datetime(2025, 4, 18, 5, 9, 1, 395648, tzinfo=datetime.timezone.utc),
                part_kind='user-prompt',
            ),
        ],
        instructions=None,
        kind='request',
    ),
    ModelResponse(
        parts=[
            TextPart(
                content=(
                    "I'll help you calculate the number of days between those two dates using Python's datetime module"
                    '.'
                ),
                part_kind='text',
            ),
            ToolCallPart(
                tool_name='run_python_code',
                args={
                    'python_code': (
                        'from datetime import date\n'
                        '\n'
                        'date1 = date(2000, 1, 1)\n'
                        'date2 = date(2025, 3, 18)\n'
                        '\n'
                        'days_between = (date2 - date1).days\n'
                        'print(f"Number of days between {date1} and {date2}: {days_between}")'
                    ),
                },
                tool_call_id='toolu_01TXXnQ5mC4ry42DrM1jPaza',
                part_kind='tool-call',
            ),
        ],
        model_name='claude-3-5-haiku-20241022',
        timestamp=datetime.datetime(2025, 4, 18, 5, 9, 12, 290782, tzinfo=datetime.timezone.utc),
        kind='response',
    ),
    ModelRequest(
        parts=[
            ToolReturnPart(
                tool_name='run_python_code',
                content=CallToolResult(
                    meta=None,
                    content=[
                        TextContent(
                            type='text',
                            text=(
                                '<status>success</status>\n'
                                '<output>\n'
                                'Number of days between 2000-01-01 and 2025-03-18: 9208\n'
                                '</output>'
                            ),
                            annotations=None,
                        ),
                    ],
                    isError=False,
                ),
                tool_call_id='toolu_01TXXnQ5mC4ry42DrM1jPaza',
                timestamp=datetime.datetime(2025, 4, 18, 5, 9, 13, 657774, tzinfo=datetime.timezone.utc),
                part_kind='tool-return',
            ),
        ],
        instructions=None,
        kind='request',
    ),
    ModelResponse(
        parts=[
            TextPart(
                content=(
                    'There are 9,208 days between January 1st, 2000, and March 18th, 2025.\n'
                    '\n'
                    'This calculation takes into account leap years and provides the exact number of days between thes'
                    'e two dates. Would you like me to break down anything else about this calculation?'
                ),
                part_kind='text',
            ),
        ],
        model_name='claude-3-5-haiku-20241022',
        timestamp=datetime.datetime(2025, 4, 18, 5, 9, 23, 411017, tzinfo=datetime.timezone.utc),
        kind='response',
    ),
]

There are 9,208 days between January 1st, 2000, and March 18th, 2025.

This calculation takes into account leap years and provides the exact number of days between these two dates. Would you like me to break down anything else about this calculation?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment