Skills.MD
# Trace Claude Code to Braintrust
Automatically send Claude Code conversations to Braintrust for tracing and observability. Get full visibility into your AI coding sessions with hierarchical traces showing sessions, turns, and every tool call.
## What you get
```
Claude Code Session (root trace)
├── Turn 1: "Add error handling"
│ ├── Read: src/app.ts
│ ├── Edit: src/app.ts
│ └── Response: "I've added try-catch..."
├── Turn 2: "Now run the tests"
│ ├── Terminal: npm test
│ └── Response: "All tests pass..."
└── Turn 3: "Great, commit this"
├── Terminal: git add .
├── Terminal: git commit -m "..."
└── Response: "Changes committed..."
```
## How it works
Four hooks capture the complete workflow:
| Hook | What it captures |
|------|------------------|
| **SessionStart** | Creates root trace when you start Claude Code |
| **PostToolUse** | Captures every tool call (file reads, edits, terminal commands) |
| **Stop** | Captures conversation turns (your message + Claude's response) |
| **SessionEnd** | Logs session summary when you exit |
## Quick setup
Run the setup script in any project directory where you want tracing:
```bash
bash /path/to/skills/trace-claude-code/setup.sh
```
The script prompts for your API key and project name, then configures all hooks automatically.
## Manual setup
### Prerequisites
- [Claude Code CLI](https://docs.anthropic.com/en/docs/claude-code) installed
- [Braintrust API key](https://www.braintrust.dev/app/settings/api-keys)
- `jq` command-line tool (`brew install jq` on macOS)
### Configuration
Create `.claude/settings.local.json` in your project directory:
```json
{
"hooks": {
"SessionStart": [
{
"hooks": [
{
"type": "command",
"command": "bash /path/to/hooks/session_start.sh"
}
]
}
],
"PostToolUse": [
{
"matcher": "*",
"hooks": [
{
"type": "command",
"command": "bash /path/to/hooks/post_tool_use.sh"
}
]
}
],
"Stop": [
{
"hooks": [
{
"type": "command",
"command": "bash /path/to/hooks/stop_hook.sh"
}
]
}
],
"SessionEnd": [
{
"hooks": [
{
"type": "command",
"command": "bash /path/to/hooks/session_end.sh"
}
]
}
]
},
"env": {
"TRACE_TO_BRAINTRUST": "true",
"BRAINTRUST_API_KEY": "sk-...",
"BRAINTRUST_CC_PROJECT": "my-project"
}
}
```
Replace `/path/to/hooks/` with the actual path to this skill's hooks directory.
### Environment variables
| Variable | Required | Description |
|----------|----------|-------------|
| `TRACE_TO_BRAINTRUST` | Yes | Set to `"true"` to enable tracing |
| `BRAINTRUST_API_KEY` | Yes | Your Braintrust API key |
| `BRAINTRUST_CC_PROJECT` | No | Project name (default: `claude-code`) |
| `BRAINTRUST_CC_DEBUG` | No | Set to `"true"` for verbose logging |
## Viewing traces
After running Claude Code with tracing enabled:
1. Go to [braintrust.dev](https://www.braintrust.dev)
2. Navigate to your project (e.g., `claude-code`)
3. Click **Logs** to see all traced sessions
Each trace shows:
- **Session root**: The overall Claude Code session
- **Turns**: Each conversation exchange (user input → assistant response)
- **Tool calls**: Individual operations (file reads, edits, terminal commands)
## Trace structure
Traces are hierarchical:
- **Session** (root span)
- `span_attributes.type`: `"task"`
- `metadata.session_id`: Unique session identifier
- `metadata.workspace`: Project directory
- **Turn** (child of session)
- `span_attributes.type`: `"llm"`
- `input`: User message
- `output`: Assistant response
- `metadata.turn_number`: Sequential turn number
- **Tool call** (child of turn or session)
- `span_attributes.type`: `"tool"`
- `input`: Tool input (file path, command, etc.)
- `output`: Tool result
- `metadata.tool_name`: Name of the tool used
## Troubleshooting
### No traces appearing
1. **Check hooks are running:**
```bash
tail -f ~/.claude/state/braintrust_hook.log
```
2. **Verify environment variables** in `.claude/settings.local.json`:
- `TRACE_TO_BRAINTRUST` must be `"true"`
- `BRAINTRUST_API_KEY` must be valid
3. **Enable debug mode:**
```json
{
"env": {
"BRAINTRUST_CC_DEBUG": "true"
}
}
```
### Permission errors
Make hook scripts executable:
```bash
chmod +x /path/to/hooks/*.sh
```
### Missing jq command
Install jq:
- **macOS**: `brew install jq`
- **Ubuntu/Debian**: `sudo apt-get install jq`
### State issues
Reset the tracing state:
```bash
rm ~/.claude/state/braintrust_state.json
```
### Hook logs
View detailed hook execution logs:
```bash
# Follow logs in real-time
tail -f ~/.claude/state/braintrust_hook.log
# View last 50 lines
tail -50 ~/.claude/state/braintrust_hook.log
# Clear logs
> ~/.claude/state/braintrust_hook.log
```
## File structure
```
hooks/
├── common.sh # Shared utilities (logging, API, state)
├── session_start.sh # Creates root trace span
├── post_tool_use.sh # Captures tool calls
├── stop_hook.sh # Captures conversation turns
└── session_end.sh # Finalizes trace
```
## Alternative: SDK integration
For programmatic use with the Claude Agent SDK, use the native Braintrust integration:
```typescript
import { initLogger, wrapClaudeAgentSDK } from "braintrust";
import * as claudeSDK from "@anthropic-ai/claude-agent-sdk";
initLogger({
projectName: "my-project",
apiKey: process.env.BRAINTRUST_API_KEY,
});
const { query, tool } = wrapClaudeAgentSDK(claudeSDK);
```
See [Braintrust Claude Agent SDK docs](https://www.braintrust.dev/docs/integrations/sdk-integrations/claude-agent-sdk) for details.
评论 0
还没有评论,来抢沙发。
相关内容
同类或同场景的收录内容
building-with-kafka-strimzi
在 Kubernetes 上构建基于 Apache Kafka 的事件驱动系统,支持高可靠的消息传递、事务性处理与精确一次语义,结合 Strimzi 运营商实现集群自动化管理,利用 Schema Registry 保障事件模式演进,适用于微服务间解耦通信与实时数据流处理场景。
站外数据与分析
609 0.0
personal-tool-builder
专注于从个人真实痛点出发快速构建实用工具,涵盖命令行程序、本地优先应用和自动化脚本的开发与演进,强调先满足自身需求再逐步扩展,通过持续自用打磨功能,避免过度设计,确保工具简洁有效且长期可维护。
站外商业与营销
196 0.0
threejs-lighting
实现三维场景中的光照与阴影控制,支持多种光源类型及环境光照明,提供从基础布光到高级阴影优化的完整方案,适用于需要真实感渲染或性能平衡的可视化项目。
站外效率工具
442 0.0
aggregation-helper
提供数据聚合场景的自动化辅助能力,涵盖 SQL 查询编写、统计分析建模、可视化方案设计及商业智能配置,支持从需求理解到生产级代码生成的全流程指导。
站外效率工具数据与分析
217 0.0
举报「trace-claude-code」
举报信息仅审核团队可见,同一资产 24 小时内仅可举报一次