Cookbook

Hello World Tracing

This recipe gets traces flowing from your code to the 2Signal dashboard in under 5 minutes. Pick your language, copy the snippet, replace the API key, and run it.

Prerequisites

  • A 2Signal account with a project and API key
  • Python 3.9+, Node.js 18+, or Go 1.21+

Python

Install the SDK and run the script:

pip install twosignal
import twosignal

client = twosignal.TwoSignal(
    api_key="your-api-key",
    base_url="https://your-instance.2signal.dev"
)

@client.observe(name="greeting-agent", span_type="AGENT")
def greet(name: str) -> str:
    return f"Hello, {name}! Welcome to 2Signal."

result = greet("Alice")
print(result)
client.flush()

What happens: A trace is created with a single AGENT span named greeting-agent. The span records the input ("Alice"), output ("Hello, Alice! ..."), timing, and status. Call client.flush() to ensure the trace is exported before the process exits. Open the Traces page in the dashboard to see it.

TypeScript

Install the SDK and run the script:

npm install twosignal
import { TwoSignal, observe } from "twosignal";

const client = new TwoSignal({
  apiKey: "your-api-key",
  baseUrl: "https://your-instance.2signal.dev",
});

const greet = observe(
  { name: "greeting-agent", spanType: "AGENT" },
  async (name: string) => {
    return `Hello, ${name}! Welcome to 2Signal.`;
  }
);

const result = await greet("Alice");
console.log(result);
client.flush();

What happens: A trace is created with a single AGENT span named greeting-agent. The span records the input, output, timing, and status. Call client.flush() to ensure the trace is exported before the process exits. Open the Traces page in the dashboard to see it.

Go

Add the module and run the program:

go get github.com/BryceKeeler720/2Signal/sdks/go
package main

import (
    "context"
    "fmt"
    twosignal "github.com/BryceKeeler720/2Signal/sdks/go"
)

func main() {
    client := twosignal.New(twosignal.Options{
        APIKey:  "your-api-key",
        BaseURL: "https://your-instance.2signal.dev",
    })
    defer client.Shutdown()

    ctx := context.Background()
    result, _ := twosignal.Observe(ctx, "greeting-agent", twosignal.SpanTypeAgent, "Alice", func(ctx context.Context, span *twosignal.Span) (string, error) {
        msg := "Hello, Alice! Welcome to 2Signal."
        span.Output = msg
        return msg, nil
    })
    fmt.Println(result)
}

What happens: A trace is created with a single AGENT span named greeting-agent. The span records the input, output, timing, and status. defer client.Shutdown() flushes all pending traces before the process exits. Open the Traces page in the dashboard to see it.

What's Next

  • Auto-Instrument LLM Calls — Wrap OpenAI, Anthropic, and other providers to capture prompts, completions, token usage, and cost automatically.
  • Evaluate Outputs — Attach evaluators to your project so every trace is scored for quality, latency, cost, and correctness.

Have questions? Join our community!

Connect with other developers and the 2Signal team.

Join Discord