Traces on Claude

I know how to make traces work with Langsmith platform when I use the langchain or deepagents but sometimes I am using things like anthropic sdk (claude -p ) or codex cli directly in my harness and I wonder is there a way to use session logs left from these tools and upload as traces to langsmith? i know i could probably use the tracable function but will I dont think it will do exactly what I want…

so long story short:

if somewhere in my harness I run: claude -p with the task and that produces session log for me - can I have this session in langsmith exactly the way how i would use langchain or deepagents?

hi @Gregradzio

have you tried Trace Claude Code applications - Docs by LangChain ?

/plugin marketplace add langchain-ai/langsmith-claude-code-plugins
/plugin install langsmith-tracing@langsmith-claude-code-plugins
/reload-plugins

then

export TRACE_TO_LANGSMITH="true"
export CC_LANGSMITH_API_KEY="<your LangSmith API key>"
export CC_LANGSMITH_PROJECT="claude-code"

claude -p "your task"

You can put the same values in .claude/settings.local.json if project-level configuration is more convenient:

{
  "env": {
    "TRACE_TO_LANGSMITH": "true",
    "CC_LANGSMITH_API_KEY": "<your LangSmith API key>",
    "CC_LANGSMITH_PROJECT": "claude-code"
  }
}

When claude -p is part of a larger traced harness (python):

import os
import subprocess

from langsmith import get_current_run_tree, traceable


@traceable(name="run_claude")
def run_claude(prompt: str) -> str:
    parent = get_current_run_tree()
    result = subprocess.run(
        ["claude", "-p", prompt],
        check=True,
        capture_output=True,
        text=True,
        env={
            **os.environ,
            "TRACE_TO_LANGSMITH": "true",
            "CC_LANGSMITH_API_KEY": os.environ["LANGSMITH_API_KEY"],
            "CC_LANGSMITH_PROJECT": "claude-code",
            "CC_LANGSMITH_PARENT_DOTTED_ORDER": parent.dotted_order,
        },
    )
    return result.stdout

or JS

import { spawn } from "node:child_process";
import { traceable, getCurrentRunTree } from "langsmith/traceable";

const runClaude = traceable(
  async (prompt: string): Promise<string> => {
    const parent = getCurrentRunTree();

    return new Promise((resolve, reject) => {
      const child = spawn("claude", ["-p", prompt], {
        env: {
          ...process.env,
          TRACE_TO_LANGSMITH: "true",
          CC_LANGSMITH_API_KEY: process.env.LANGSMITH_API_KEY!,
          CC_LANGSMITH_PROJECT: "claude-code",
          CC_LANGSMITH_PARENT_DOTTED_ORDER: parent.dotted_order,
        },
      });

      let stdout = "";
      let stderr = "";

      child.stdout.on("data", chunk => (stdout += chunk));
      child.stderr.on("data", chunk => (stderr += chunk));

      child.on("error", reject);
      child.on("close", code => {
        if (code === 0) resolve(stdout);
        else reject(new Error(`Claude exited with code ${code}: ${stderr}`));
      });
    });
  },
  { name: "run_claude" },
);

await runClaude("Your task");

actually yes - i had to do tiny tweaks for my eval harness but it worked, thanks