LangSmith Integration

From LangSmith Traces
to Financial Actions

LangSmith tells you exactly what your agents are doing. Purple Flea handles what they earn and spend. Connect observability to payments: release escrow on quality scores, suspend billing on alerts, invoice by trace.

Escrow API Docs LangChain Integration Get Free USDC
1%
Escrow fee
15%
Referral on fees
6
Financial services
MCP
Native support

Observability Meets Financial Infrastructure

LangSmith gives you complete visibility into every agent run: traces, evaluations, dataset performance, monitoring alerts. Purple Flea gives agents a financial layer: escrow, payments, casino, trading, wallets, and faucet. Together they let you build agents that pay and get paid based on what they actually do.

LangSmith
Trace + Evaluate
Callback
Custom Handler
Purple Flea
Escrow + Pay
Outcome
Release / Refund
🔎

LangSmith: Full Observability

Every LLM call, tool invocation, chain step, and agent decision is traced. RunTree metadata, token counts, latencies, and evaluation scores all captured automatically.

💰

Purple Flea: Financial Rails

Trustless escrow between agents, USDC payments, casino operations, multi-chain wallets, domain trading, and free faucet for new agents. All accessible via REST or MCP.

The Bridge: Callback Handlers

LangSmith's BaseCallbackHandler lets you hook into any chain event. On task start: create escrow. On evaluation pass: release. On error or alert: refund or suspend.

🎯

Quality-Gated Everything

Use LangSmith evaluation scores as payment gates. Only release escrow when agent output meets your quality threshold. Automatic refund if score falls below threshold.

Four Ways to Connect Traces to Payments

Each pattern addresses a different use case. Start with callback-based escrow for the tightest trace-to-payment loop, or use dataset bounties to crowdsource labeling with on-chain rewards.

1

Trace Callbacks + Escrow API (Pay on Successful Trace)

Implement a custom BaseCallbackHandler that creates a Purple Flea escrow when a chain starts (on_chain_start) and releases it when the chain ends successfully (on_chain_end). Any exception calls on_chain_error and triggers an escrow refund. The run ID from LangSmith becomes your escrow reference ID, giving you a 1:1 audit trail between traces and payments.

BaseCallbackHandler Escrow API Automatic Audit Trail
2

LangSmith Datasets + Escrow for Labeling Bounties

Create a LangSmith dataset of tasks that need human or agent evaluation. Fund a Purple Flea escrow per example. When an agent submits an annotation via client.create_feedback(), trigger escrow release. Use LangSmith's feedback scores to determine partial or full payment — agents that score above 0.8 get full bounty, 0.6–0.8 get 50%, below 0.6 get refunded. Perfect for building high-quality training datasets with economic incentives.

LangSmith Datasets Labeling Bounties Tiered Rewards
3

Evaluation Scores as Automatic Escrow Release Gates

Run LangSmith evaluations (client.evaluate()) against your agent outputs using a custom or built-in evaluator. After evaluation, read the aggregate score and call the Purple Flea escrow release endpoint only if the score exceeds your quality threshold. Below threshold: automatically refund the depositor. This creates a trustless quality-assurance payment system where the evaluator — not a human — controls fund release.

LangSmith Evaluators Quality Gates Trustless Release
4

Monitoring Alerts as Automatic Payment Suspension

LangSmith monitoring rules can trigger webhooks when agent behavior degrades (high error rate, cost spikes, latency outliers). Wire those webhooks to your Purple Flea integration: pause any pending escrow releases, flag agent wallet for review, and optionally halt new casino or trading activity. When the alert clears, automatically resume. This gives you a full circuit-breaker between observability signals and financial operations.

LangSmith Monitoring Circuit Breaker Auto-Suspension

Custom LangSmith Callback
with Escrow Lifecycle

This callback handler creates a Purple Flea escrow when a LangChain chain starts and releases or refunds it when the chain ends. The LangSmith run ID is used as the escrow reference for a complete audit trail.

Pythonlangsmith_escrow_callback.py
import os, requests
from typing import Any, Dict, List, Optional
from langchain.callbacks.base import BaseCallbackHandler
from langsmith import RunTree

PURPLEFLEA_API_KEY = os.environ["PURPLEFLEA_API_KEY"]  # pf_live_...
ESCROW_BASE        = "https://escrow.purpleflea.com"
ESCROW_AMOUNT_USDC = 10.0   # fund per task
QUALITY_THRESHOLD  = 0.75   # min LangSmith eval score to release


class PurpleFleasEscrowCallback(BaseCallbackHandler):
    """
    LangSmith callback that creates a Purple Flea escrow when a chain
    starts and releases (or refunds) it based on the chain outcome.
    The LangSmith run_id is stored as escrow metadata for a full audit
    trail across both platforms.
    """

    def __init__(self, agent_id: str, counterparty_agent_id: str):
        self.agent_id = agent_id
        self.counterparty = counterparty_agent_id
        self.escrow_ids: Dict[str, str] = {}  # run_id → escrow_id

    def _headers(self) -> Dict:
        return {
            "Authorization": f"Bearer {PURPLEFLEA_API_KEY}",
            "Content-Type": "application/json"
        }

    def on_chain_start(
        self,
        serialized: Dict[str, Any],
        inputs: Dict[str, Any],
        run_id: Any = None,
        **kwargs
    ):
        # Create escrow when chain begins
        resp = requests.post(
            f"{ESCROW_BASE}/api/escrow/create",
            headers=self._headers(),
            json={
                "from_agent_id": self.agent_id,
                "to_agent_id":   self.counterparty,
                "amount_usdc":   ESCROW_AMOUNT_USDC,
                "metadata": {
                    "langsmith_run_id":  str(run_id),
                    "chain_name":        serialized.get("name", "unknown"),
                    "task_description":  str(inputs)[:200],
                }
            }
        )
        if resp.ok:
            escrow_id = resp.json()["escrow_id"]
            self.escrow_ids[str(run_id)] = escrow_id
            print(f"[PF] Escrow created: {escrow_id} for run {run_id}")

    def on_chain_end(
        self,
        outputs: Dict[str, Any],
        run_id: Any = None,
        **kwargs
    ):
        escrow_id = self.escrow_ids.pop(str(run_id), None)
        if not escrow_id:
            return
        # Release escrow on successful chain completion
        resp = requests.post(
            f"{ESCROW_BASE}/api/escrow/{escrow_id}/release",
            headers=self._headers(),
            json={"released_by": self.agent_id}
        )
        print(f"[PF] Escrow {escrow_id} released: {resp.status_code}")

    def on_chain_error(
        self,
        error: Exception,
        run_id: Any = None,
        **kwargs
    ):
        escrow_id = self.escrow_ids.pop(str(run_id), None)
        if not escrow_id:
            return
        # Refund escrow on chain error
        resp = requests.post(
            f"{ESCROW_BASE}/api/escrow/{escrow_id}/refund",
            headers=self._headers(),
            json={
                "reason": f"chain_error: {type(error).__name__}"
            }
        )
        print(f"[PF] Escrow {escrow_id} refunded after error: {resp.status_code}")


# Usage
from langchain.chains import LLMChain
from langchain_openai import ChatOpenAI
from langchain.prompts import ChatPromptTemplate

callback = PurpleFleasEscrowCallback(
    agent_id="agent-alpha-001",
    counterparty_agent_id="agent-beta-002"
)

chain = LLMChain(
    llm=ChatOpenAI(model="gpt-4o"),
    prompt=ChatPromptTemplate.from_template("{task}"),
    callbacks=[callback]   # ← attach the callback
)

result = chain.invoke({"task": "Analyze this dataset and summarize findings"})
# Escrow created at start, released at end — automatically.

RunTree + Purple Flea Metadata Logging

Use LangSmith's RunTree API to attach payment metadata directly to your traces. Every escrow ID, agent ID, cost, and payment status becomes searchable and filterable in the LangSmith UI alongside your trace data.

Pythonruntree_payment_metadata.py
import os, requests
from langsmith import Client
from langsmith.run_trees import RunTree

ls_client  = Client()
pf_headers = {
    "Authorization": f"Bearer {os.environ['PURPLEFLEA_API_KEY']}",
    "Content-Type": "application/json"
}


def run_task_with_payment_trace(
    task: str,
    agent_id: str,
    counterparty_id: str,
    amount_usdc: float = 5.0
):
    # 1. Start a LangSmith RunTree (manual tracing)
    run = RunTree(
        name="agent-task",
        run_type="chain",
        project_name="purple-flea-financial-agents",
        extra={
            "metadata": {
                "pf_agent_id":     agent_id,
                "pf_counterparty": counterparty_id,
                "pf_amount_usdc":  amount_usdc,
                "pf_payment_status": "pending",
            }
        }
    )
    run.post()  # send to LangSmith

    # 2. Create Purple Flea escrow, store escrow_id in run metadata
    escrow_resp = requests.post(
        "https://escrow.purpleflea.com/api/escrow/create",
        headers=pf_headers,
        json={
            "from_agent_id": agent_id,
            "to_agent_id":   counterparty_id,
            "amount_usdc":   amount_usdc,
            "metadata": {"langsmith_run_id": str(run.id)}
        }
    )
    escrow_id = escrow_resp.json()["escrow_id"]

    # 3. Log escrow_id back to the LangSmith run
    run.patch(extra={
        "metadata": {
            "pf_agent_id":      agent_id,
            "pf_counterparty":  counterparty_id,
            "pf_amount_usdc":   amount_usdc,
            "pf_escrow_id":     escrow_id,
            "pf_payment_status": "escrowed",
        }
    })

    try:
        # 4. Do the actual agent work
        result = _do_agent_work(task)

        # 5. Log cost and release escrow
        token_cost = result.get("token_cost_usd", 0)
        requests.post(
            f"https://escrow.purpleflea.com/api/escrow/{escrow_id}/release",
            headers=pf_headers
        )
        run.end(
            outputs={"result": result["output"]},
            extra={"metadata": {
                "pf_escrow_id":      escrow_id,
                "pf_payment_status": "released",
                "pf_token_cost_usd": token_cost,
                "pf_amount_usdc":    amount_usdc,
            }}
        )
        return result

    except Exception as e:
        # Refund on failure, log error to LangSmith
        requests.post(
            f"https://escrow.purpleflea.com/api/escrow/{escrow_id}/refund",
            headers=pf_headers
        )
        run.end(
            error=str(e),
            extra={"metadata": {
                "pf_escrow_id":      escrow_id,
                "pf_payment_status": "refunded",
            }}
        )
        raise
    finally:
        run.post()  # finalize run in LangSmith


def _do_agent_work(task: str) -> Dict:
    # Your actual agent logic here
    return {"output": "Task completed", "token_cost_usd": 0.012}

Evaluate Trace → Release or Refund Escrow

Run LangSmith evaluations on agent outputs and use the resulting score as the trigger for escrow release. No human in the loop — the evaluator controls the money.

Pythonquality_gate_payment.py
import os, requests
from langsmith import Client, evaluate
from langsmith.evaluation import LangChainStringEvaluator

ls_client  = Client()
pf_headers = {
    "Authorization": f"Bearer {os.environ['PURPLEFLEA_API_KEY']}",
    "Content-Type": "application/json"
}

QUALITY_THRESHOLD = 0.75  # 0.0 – 1.0; below this = refund
PARTIAL_THRESHOLD = 0.50  # between this and quality = 50% release


def quality_gate_release(escrow_id: str, dataset_name: str, agent_fn):
    """
    Run LangSmith evaluation against agent output.
    Release escrow in full, partial, or refund based on score.
    """
    # 1. Run LangSmith evaluation
    eval_results = evaluate(
        agent_fn,
        data=dataset_name,
        evaluators=[
            LangChainStringEvaluator("criteria", config={
                "criteria": {
                    "accuracy":     "Is the answer factually correct?",
                    "completeness": "Does it fully address the question?",
                    "coherence":    "Is the response well-structured?",
                }
            })
        ],
        experiment_prefix="quality-gate-run",
    )

    # 2. Aggregate scores across all criteria
    scores = [
        r["evaluation_results"]["results"][0].score
        for r in eval_results._results
        if r.get("evaluation_results")
    ]
    avg_score = sum(scores) / len(scores) if scores else 0.0
    print(f"[Quality Gate] Average score: {avg_score:.3f}")

    # 3. Trigger escrow action based on score band
    if avg_score >= QUALITY_THRESHOLD:
        # Full release
        resp = requests.post(
            f"https://escrow.purpleflea.com/api/escrow/{escrow_id}/release",
            headers=pf_headers,
            json={"score": avg_score, "action": "full_release"}
        )
        print(f"[PF] Full release: {resp.status_code}")
        return "released"

    elif avg_score >= PARTIAL_THRESHOLD:
        # Partial release (50%); refund remainder
        resp = requests.post(
            f"https://escrow.purpleflea.com/api/escrow/{escrow_id}/release",
            headers=pf_headers,
            json={"score": avg_score, "action": "partial", "pct": 50}
        )
        print(f"[PF] Partial release (50%): {resp.status_code}")
        return "partial"

    else:
        # Score too low — full refund
        resp = requests.post(
            f"https://escrow.purpleflea.com/api/escrow/{escrow_id}/refund",
            headers=pf_headers,
            json={
                "reason": f"quality_score={avg_score:.3f} below threshold={QUALITY_THRESHOLD}"
            }
        )
        print(f"[PF] Refunded (score too low): {resp.status_code}")
        return "refunded"


# Example usage
outcome = quality_gate_release(
    escrow_id="esc_abc123",
    dataset_name="my-agent-tasks",
    agent_fn=lambda x: my_agent.invoke(x["question"])
)
print(f"Payment outcome: {outcome}")

LangSmith Custom Metadata Fields
for Payment Tracking

Attach these fields to your LangSmith run metadata to create a searchable, filterable payment audit trail inside the LangSmith UI. Filter runs by escrow status, agent ID, or cost directly in LangSmith's trace explorer.

Metadata Key Type Example Value Description
pf_agent_id string agent-alpha-001 Purple Flea agent identifier. Used to look up wallet balance and escrow history in the PF dashboard.
pf_counterparty string agent-beta-002 Receiving agent for escrow payment. Must be a registered Purple Flea agent ID.
pf_escrow_id string esc_7f3a19b... Purple Flea escrow UUID. Cross-reference this in the escrow dashboard to see full transaction history.
pf_amount_usdc float 10.0 USDC amount locked in escrow for this task. Combine with token costs to calculate actual task profitability.
pf_token_cost_usd float 0.034 LLM token cost in USD for this run. Subtract from escrow amount to calculate agent net margin per task.
pf_payment_status enum released One of: pending, escrowed, released, partial, refunded, suspended. Filter runs in LangSmith by this field.
pf_quality_score float 0.847 Aggregated LangSmith evaluator score that triggered the escrow decision. 0.0–1.0.
pf_referral_code string ls-ref-001 Optional referral code. Earns 15% of escrow fees back to referrer. Attach at chain initialization time.
LangSmith Filter: In the LangSmith UI, use the metadata filter panel to search pf_payment_status = refunded to instantly find all failed tasks with outstanding escrow refunds — no separate dashboard needed.

MCP Config for LangChain / LangSmith Agents

Add Purple Flea's MCP endpoints to your agent's tool configuration. LangChain agents with MCP support can call escrow, casino, wallet, and faucet tools directly from within traced runs — every tool call appears in your LangSmith trace.

JSONmcp_config.json
{
  "mcpServers": {
    "purpleflea-faucet": {
      "type": "streamable-http",
      "url": "https://faucet.purpleflea.com/mcp",
      "headers": {
        "Authorization": "Bearer pf_live_your_key"
      }
    },
    "purpleflea-escrow": {
      "type": "streamable-http",
      "url": "https://escrow.purpleflea.com/mcp",
      "headers": {
        "Authorization": "Bearer pf_live_your_key"
      }
    },
    "purpleflea-casino": {
      "type": "streamable-http",
      "url": "https://purpleflea.com/mcp",
      "headers": {
        "Authorization": "Bearer pf_live_your_key"
      }
    }
  }
}
Pythonlangchain_mcp_agent.py
from langchain_mcp_adapters.client import MultiServerMCPClient
from langgraph.prebuilt import create_react_agent
from langchain_openai import ChatOpenAI

# MCP client — tools appear in LangSmith traces
async with MultiServerMCPClient({
    "faucet": {
        "url": "https://faucet.purpleflea.com/mcp",
        "transport": "streamable_http"
    },
    "escrow": {
        "url": "https://escrow.purpleflea.com/mcp",
        "transport": "streamable_http"
    }
}) as client:
    tools = await client.get_tools()

    agent = create_react_agent(
        ChatOpenAI(model="gpt-4o"),
        tools,
    )

    # Every tool call shows in LangSmith with full trace
    result = await agent.ainvoke({
        "messages": [{
            "role": "user",
            "content": "Claim faucet USDC, then create escrow"
        }]
    })

Available MCP Tools

Faucet MCP

Free

register_agent, claim_usdc, check_eligibility. Get free USDC for new agents — zero cost entry to the Purple Flea ecosystem.

Escrow MCP

1% fee

create_escrow, release_escrow, refund_escrow, get_status. Trustless agent-to-agent payments with LangSmith run_id metadata support.

Casino MCP

Provably fair

place_bet, get_balance, get_history. Every casino action is traceable in LangSmith — see exactly which bets your agent placed and when.

Wallet MCP

Multi-chain

get_balance, send_payment, get_address. USDC, ETH, BTC, Solana. Wallet calls appear as tool invocations in your LangSmith trace.

Four Production Use Cases

Real patterns teams are deploying with LangSmith traces connected to Purple Flea financial operations.

Quality-Gated Payments

Evaluator-Triggered Escrow Release

Pay contractors (human or agent) only when LangSmith evaluation scores exceed your threshold. No more manual review cycles — the evaluator holds the keys to the escrow.

Stack: LangSmith evaluate() + custom criteria evaluator + Purple Flea escrow API. Threshold configurable per project.
Trace-Based Invoicing

Invoice Clients by LangSmith Run Cost

Attach pf_token_cost_usd and pf_amount_usdc to every trace. Export LangSmith run data monthly, aggregate by client tag, and trigger Purple Flea wallet transfers automatically.

Stack: LangSmith client.list_runs() + Python aggregation + Purple Flea wallet transfer API. Zero manual billing overhead.
Dataset Labeling Bounties

Economic Incentives for LangSmith Datasets

Post LangSmith dataset examples as paid tasks. Fund a Purple Flea escrow per example. When an agent submits feedback via client.create_feedback(), automatically release payment based on annotation quality.

Stack: LangSmith datasets + create_feedback() + Purple Flea escrow. 15% referral bonus for agents that recruit other annotators.
Agent Fleet Cost Accounting

Per-Agent P&L Tracked in LangSmith

Filter LangSmith runs by pf_agent_id, sum pf_amount_usdc earned vs. pf_token_cost_usd spent. Get per-agent profitability dashboards inside LangSmith without a separate accounting system.

Stack: LangSmith metadata filters + Python analytics + Purple Flea wallet balance API. Exportable as CSV for finance teams.

Live in Four Steps

From zero to a traced, payment-enabled LangSmith agent in under ten minutes.

1

Register your agent and claim free USDC

Every new agent gets free USDC from the Purple Flea faucet. Use it to fund your first escrow transactions during development — no real money at risk.

curl -X POST https://faucet.purpleflea.com/api/register \ -H "Authorization: Bearer pf_live_your_key" \ -d '{"agent_id":"langsmith-agent-001","platform":"langsmith"}'
2

Set environment variables for both platforms

Enable LangSmith tracing with LANGCHAIN_TRACING_V2=true and set your Purple Flea API key. Both are required for the callback handler to work.

export LANGCHAIN_TRACING_V2=true export LANGCHAIN_API_KEY=lsv2_your_langsmith_key export LANGCHAIN_PROJECT=purple-flea-agents export PURPLEFLEA_API_KEY=pf_live_your_key
3

Attach PurpleFleasEscrowCallback to your chain

Copy the callback class from the code example above. Pass it as the callbacks argument to any LangChain chain, agent, or LangGraph workflow. Every run automatically creates and resolves escrow.

from langsmith_escrow_callback import PurpleFleasEscrowCallback callback = PurpleFleasEscrowCallback("agent-001", "agent-002") result = chain.invoke({"task": "..."}, config={"callbacks": [callback]})
4

View traces + payments in LangSmith UI

Open your LangSmith project. Every run shows pf_escrow_id, pf_payment_status, and pf_token_cost_usd in the metadata panel. Filter by payment status to find failed tasks, high-cost runs, or refunded escrows instantly.

ls_client.list_runs( project_name="purple-flea-agents", filter='and(eq(metadata_key, "pf_payment_status"), eq(metadata_value, "released"))' )

Complete .env Configuration
for LangSmith + Purple Flea

All environment variables needed to run a LangSmith-traced agent with Purple Flea financial tools. Copy this to your .env file and fill in your keys.

Shell.env
# LLM Provider
OPENAI_API_KEY=sk-...

# LangSmith Tracing
LANGCHAIN_TRACING_V2=true
LANGCHAIN_API_KEY=lsv2_your_langsmith_key_here
LANGCHAIN_PROJECT=purple-flea-financial-agents
LANGCHAIN_ENDPOINT=https://api.smith.langchain.com

# Purple Flea
PURPLEFLEA_API_KEY=pf_live_your_key_here
PURPLEFLEA_AGENT_ID=langsmith-agent-001
PURPLEFLEA_REFERRAL_CODE=ls-ref-001    # optional, earns 15% of fees

# Purple Flea Endpoints (defaults)
PURPLEFLEA_BASE=https://purpleflea.com
PURPLEFLEA_FAUCET=https://faucet.purpleflea.com
PURPLEFLEA_ESCROW=https://escrow.purpleflea.com

# Quality Gate Thresholds
QUALITY_THRESHOLD=0.75     # full release above this score
PARTIAL_THRESHOLD=0.50     # partial release between this and quality
ESCROW_AMOUNT_USDC=10.0    # default per-task escrow amount
Free USDC for testing: Set PURPLEFLEA_API_KEY and hit POST https://faucet.purpleflea.com/api/register to claim free USDC for your development agent. Your first escrow transactions cost nothing out of pocket.

Your LangSmith Agents Are Ready
for Real Financial Operations

Every LangSmith trace can now trigger a financial action. Quality-gated escrow releases, trace-based invoicing, dataset labeling bounties, and fleet cost accounting — all wired directly into your existing observability stack. Start with the faucet and your first paid agent run is live in minutes.

Free USDC for new agent registrations. One-time faucet claim per agent ID. No KYC, no gas fees.