Overview
chainloop trace hooks into Claude Code and Git to automatically capture what happens during AI-assisted coding sessions — models used, tokens consumed, tools invoked, code changes produced, and more. Every git push bundles this data into a cryptographically signed attestation using the CHAINLOOP_AI_CODING_SESSION material type.
This gives security and engineering teams full visibility into how AI agents contribute to your codebase, without changing how developers work.
chainloop trace currently supports Claude Code sessions. Support for additional AI coding agents is planned.Why Trace AI Coding Sessions
AI coding agents are productive — but without visibility, teams can’t answer basic questions:- Which commits were AI-assisted, and what models were used?
- How much did a session cost in tokens and dollars?
- What tools did the agent invoke, and how many times?
- What code changes did the agent produce?
chainloop trace answers all of these automatically. Combined with Chainloop’s policy engine, you can enforce governance rules — for example, requiring that AI-assisted commits use approved models or stay within token budgets.
How It Works
chainloop trace init installs lightweight hooks into both Git and Claude Code that work together:
- When a Claude Code session starts, a hook records the session and begins capturing session data. As the agent edits files, chainloop trace monitors every change — snapshotting files before and after each edit to record exactly which lines the AI modified.
- When you commit, a hook detects which sessions contributed to the commit. A single commit can include changes from multiple sessions — or a mix of AI and human edits.
- When you push, a pre-push hook builds the full evidence: session metadata, token usage, tool invocations, and per-line code attribution showing which lines were written by AI vs human. The evidence is signed and pushed to Chainloop as an attestation.

Getting Started
Prerequisites
- Chainloop CLI installed and authenticated
- A Chainloop project to associate traces with
- Git repository with Claude Code configured
Initialize Tracing
Runchainloop trace init from your repository root:
- Creates the
.git/chainloop-trace/directory for local state - Installs Git hooks (
post-commitandpre-push) - Installs Claude Code hooks (
SessionStart,PreToolUse,PostToolUse, andSessionEnd) into.claude/settings.json
.chainloop.yml file with a projectName field, you can omit the --project flag — the CLI resolves it automatically.
Work as Usual
Once initialized, there’s nothing else to do. Write code with Claude Code, commit, and push. The hooks handle everything in the background.Remove Tracing
To uninstall all hooks and clean up local state:.claude/settings.json, and the .git/chainloop-trace/ directory. If existing hooks were backed up during installation, they are restored.
How Attribution Works
Chainloop trace goes beyond tracking which commits are AI-assisted — it knows exactly which lines of code were written by AI and which by a human.Per-Line Tracking
Every time an AI agent edits a file (viaEdit, Write, or MultiEdit tools), chainloop trace captures a snapshot before and after the change. By diffing these snapshots, it records the exact line ranges the AI modified — giving you line-level attribution for every file.
Multiple Sessions per Commit
A single commit can include changes from multiple AI sessions, or a mix of AI and human edits. Chainloop trace detects all contributing sessions automatically — you don’t need to commit separately for each session. For example, if session A editshandler.go, session B edits config.go, and you commit both together, the evidence correctly attributes each file to its respective session.
Attribution at Push Time
When you push, chainloop trace combines the per-line tracking data with the git diff to produce the final attribution:- Each file gets a label:
aiorhuman - AI-attributed files include the exact line ranges the agent touched
- Aggregate stats break down total lines added/removed by AI vs human
What Gets Captured
TheCHAINLOOP_AI_CODING_SESSION evidence includes:
Session Metadata
- Session ID, start/end time, and duration
- Claude Code version and session slug
Model and Token Usage
- Primary model and all models used during the session
- Input, output, and cache tokens
- Estimated cost in USD (based on published Anthropic pricing)
Tool Usage
- Every tool the agent invoked (Read, Write, Edit, Bash, Grep, etc.)
- Invocation count per tool and total invocations
Conversation Summary
- Total messages, user messages, and assistant messages
Subagent Details
- Each subagent spawned: type, description, and token usage
Git Context
- Repository URL, branch, and working directory
- Commit range (start SHA to end SHA) and full list of commits
- Merge-base detection against main/master
Code Changes
- Files modified, created, deleted, renamed, or copied
- Lines added and removed (per-file and aggregate)
Code Attribution
- Per-file attribution label:
aiorhuman - Exact line ranges the AI agent touched (1-indexed, inclusive)
- Session IDs: which sessions modified each file
- Aggregate breakdown: AI lines added/removed vs human lines added/removed
Evidence Structure
Applying Policies
DefineCHAINLOOP_AI_CODING_SESSION in your contract to attach policies to traced sessions:
contract.yaml
Example: Restrict to Approved Models
check-approved-models.yaml
Example: Enforce Token Budget
check-token-budget.yaml
Example: Limit AI-Authored Code Ratio
check-ai-code-ratio.yaml
Inspecting Traces in the Platform
Once a trace attestation has been pushed, you can inspect it directly in the Chainloop Web UI. Navigate to the workflow run that contains theCHAINLOOP_AI_CODING_SESSION material.
Rendered View
The platform renders a structured summary of the session — model usage, token consumption, estimated cost, tool invocations, code changes, and per-line attribution — all in an easy-to-read format.

Raw View
Switch to the raw view to see the full JSON evidence as captured by the hooks. This is useful for debugging policies or understanding the exact data available for Rego evaluation.
Relationship to AI Config Collection
chainloop trace and the AI config collector are complementary:
| AI Config Collector | Chainloop Trace | |
|---|---|---|
| What it captures | Static configuration files (CLAUDE.md, settings, MCP config, rules, skills) | Runtime session data (tokens, tools, code changes, costs) |
| When it runs | During chainloop attestation init --collectors aiagent | Automatically on every git push via hooks |
| Material type | CHAINLOOP_AI_AGENT_CONFIG | CHAINLOOP_AI_CODING_SESSION |
| Use case | Governance over how agents are configured | Visibility into what agents actually did |
Related Resources
- How to collect AI agent configuration — Collect static AI agent configuration files
- Material Types — Full list of supported material types
- How to write custom policies — Write Rego policies for your evidence
- Policies concept — How policies work in Chainloop
