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.
In this repository65 other files it gives its agents.
- AGENTS.mdAGENTS.md
- skills/building-agents/agents.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- llms-full.txtllms.txt
- llms.txtllms.txt
- adding-guardrailsSkill
- agent-health-timeoutSkill
- agent-permission-scopingSkill
- agent-session-isolationSkill
- agent-supervisor-routerSkill
- agent-telemetrySkill
- async-agent-backgroundSkill
- building-agentsSkill
- building-teamsSkill
- building-toolsSkill
- calendar-scheduling-toolSkill
- callback-tracing-handlerSkill
- chain-of-thought-promptingSkill
- chat-model-configurationSkill
- code-execution-sandboxSkill
- common-agent-patternsSkill
- context-window-managementSkill
- conversation-memorySkill
- conversation-state-dbSkill
- database-query-toolSkill
- document-loadersSkill
- document-rerankingSkill
- email-notification-toolSkill
- embedding-model-ingestionSkill
- fallback-model-chainSkill
- few-shot-injectionSkill
- filesystem-toolSkill
- human-in-the-loopSkill
- hybrid-searchSkill
- inspecting-streamsSkill
- integrating-with-laurenSkill
- json-schema-enforcementSkill
- llm-provider-connectionSkill
- llm-rate-limitingSkill
- long-term-vector-memorySkill
- managing-memorySkill
- migrating-to-lauren-aiSkill
- multi-agent-delegationSkill
- multi-tool-orchestrationSkill
- multi-turn-tool-stateSkill
- output-guardrailsSkill
- plan-and-execute-agentSkill
- prompt-injection-defenseSkill
- prompt-versioningSkill
- rag-pipelineSkill
- react-agent-loopSkill
- response-validation-retrySkill
- rest-api-toolSkill
- securing-agentsSkill
- semantic-cacheSkill
- streaming-response-handlerSkill
- structured-output-parsingSkill
- sub-agent-spawningSkill
- system-prompt-templatingSkill
- testing-agentsSkill
- The first 60 shown.
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
- Distinctive: line 65
Use a list-based handler to assert on log output without configuring a real logging destination:
≈ “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.
Files
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
| Hook | Parameters | Called 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 | None | After 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_resultreturningNoneleaves the result unchanged; returning a newToolResultreplaces it (useful for sanitising sensitive outputs).- Hooks run synchronously or asynchronously — both
defandasync defwork. - The runner swallows hook exceptions (logging a warning) so a broken hook never aborts the agent run.
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.

