agentleFS
lauren-frameworklauren-framework/lauren-ai/skills/agent-step-logging/SKILL.md

agent-step-logging

Skill0 starsChanged 6 months agoMITView on GitHub

Log each agentic loop step using lifecycle hooks on the @agent class. Use when you need structured per-step logs for debugging, auditing, or monitoring without modifying the runner or adding external infrastructure.

The file

We split this skill into the rules it gives an agent

1 rule. About 0 are common, 1 is distinctive, and 0 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Use a list-based handler to assert on log output without configuring a real logging destination:

    line 65

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Use codemap find "AgentContext" to check the lifecycle hook signatures.

Agent Execution Step Logging

Define on_start, on_turn_complete, on_tool_result, and on_finish lifecycle hooks on your @agent()-decorated class to log each step.

Hook signatures

HookParametersCalled when
on_start(ctx: AgentContext)Before first LLM call
on_turn_complete(completion: Completion, ctx: AgentContext)After each LLM call
on_tool_result(result: ToolResult, ctx: AgentContext) -> ToolResult | NoneAfter each tool execution
on_finish(response: AgentResponse, ctx: AgentContext)After the run completes

Pattern

import logging
from lauren_ai import agent, use_tools
from lauren_ai._agents import AgentContext, AgentResponse
from lauren_ai._transport import Completion
from lauren_ai._tools import ToolResult

logger = logging.getLogger(__name__)

@agent(model="claude-sonnet-4-6", system="You are a helpful assistant.")
class LoggingAgent:
    async def on_start(self, ctx: AgentContext) -> None:
        logger.info("Agent started: run_id=%s turn=%d", ctx.agent_run_id, ctx.turn)

    async def on_turn_complete(self, completion: Completion, ctx: AgentContext) -> None:
        logger.info(
            "Turn %d complete: stop_reason=%s tokens_out=%d",
            ctx.turn,
            completion.stop_reason,
            completion.usage.output_tokens,
        )

    async def on_tool_result(self, result: ToolResult, ctx: AgentContext) -> ToolResult | None:
        logger.info(
            "Tool %s result: is_error=%s",
            result.tool_use_id,
            result.is_error,
        )
        return None  # return None to leave result unchanged

    async def on_finish(self, response: AgentResponse, ctx: AgentContext) -> None:
        logger.info(
            "Agent finished: turns=%d total_in=%d total_out=%d",
            response.turns,
            response.total_usage.input_tokens,
            response.total_usage.output_tokens,
        )

Capture logs in tests

Use a list-based handler to assert on log output without configuring a real logging destination:

import logging

class ListHandler(logging.Handler):
    def __init__(self):
        super().__init__()
        self.records: list[str] = []
    def emit(self, record):
        self.records.append(self.format(record))

handler = ListHandler()
logging.getLogger("myapp.agents").addHandler(handler)
logging.getLogger("myapp.agents").setLevel(logging.INFO)

Or use pytest's built-in caplog fixture:

@pytest.mark.asyncio
async def test_hooks_called(caplog):
    with caplog.at_level(logging.INFO):
        response = await runner.run(agent_instance, "Hello")
    assert any("Agent started" in r for r in caplog.messages)
    assert any("Agent finished" in r for r in caplog.messages)

Notes

  • Hooks are optional — define only the ones you need.
  • on_tool_result returning None leaves the result unchanged; returning a new ToolResult replaces it (useful for sanitising sensitive outputs).
  • Hooks run synchronously or asynchronously — both def and async def work.
  • The runner swallows hook exceptions (logging a warning) so a broken hook never aborts the agent run.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

Reviews can't be read right now.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.