Replicate (tracing)

Replicate is a platform for running machine learning models in the cloud. It hosts thousands of open-source models and provides a simple API for running predictions without managing infrastructure.

Create an account at platform.respan.ai and grab an API key.

Run npx @respan/cli setup to set up with your coding agent.

See Replicate gateway setup to route Replicate calls through the Respan gateway.

Setup

1

Install packages

$pip install respan-ai respan-instrumentation-replicate replicate
2

Set environment variables

$export REPLICATE_API_TOKEN="YOUR_REPLICATE_API_TOKEN"
$export RESPAN_API_KEY="YOUR_RESPAN_API_KEY"

REPLICATE_API_TOKEN is used for Replicate predictions. RESPAN_API_KEY is used to export traces to Respan.

3

Initialize and run

1import os
2
3import replicate
4from respan import Respan, workflow
5from respan_instrumentation_replicate import ReplicateInstrumentor
6
7respan = Respan(
8 api_key=os.environ["RESPAN_API_KEY"],
9 instrumentations=[ReplicateInstrumentor()],
10)
11
12
13@workflow(name="replicate_quickstart.workflow")
14def run_prediction() -> str:
15 output = replicate.run(
16 "meta/meta-llama-3-8b-instruct",
17 input={"prompt": "Reply with one concise sentence about tracing."},
18 )
19 return "".join(str(chunk) for chunk in output) if not isinstance(output, str) else output
20
21
22print(run_prediction())
23respan.flush()
24respan.shutdown()
4

View your trace

Open the Traces page and search for workflow name replicate_quickstart.workflow.

Configuration

ParameterTypeDefaultDescription
api_keystr | NoneNoneFalls back to RESPAN_API_KEY env var.
base_urlstr | NoneNoneFalls back to RESPAN_BASE_URL env var.
instrumentationslist[]Plugin instrumentations to activate (e.g. ReplicateInstrumentor()).
customer_identifierstr | NoneNoneDefault customer identifier for all spans.
metadatadict | NoneNoneDefault metadata attached to all spans.
environmentstr | NoneNoneEnvironment tag (e.g. "production").

Attributes

In Respan()

1from respan import Respan
2from respan_instrumentation_replicate import ReplicateInstrumentor
3
4respan = Respan(
5 instrumentations=[ReplicateInstrumentor()],
6 customer_identifier="user_123",
7 metadata={"service": "replicate-api", "version": "1.0.0"},
8)

With propagate_attributes

1import replicate
2
3from respan import Respan, propagate_attributes
4from respan_instrumentation_replicate import ReplicateInstrumentor
5
6respan = Respan(instrumentations=[ReplicateInstrumentor()])
7
8
9def handle_request(user_id: str, prompt: str):
10 with propagate_attributes(
11 customer_identifier=user_id,
12 thread_identifier="conv_abc_123",
13 metadata={"plan": "pro"},
14 ):
15 output = replicate.run("meta/meta-llama-3-8b-instruct", input={"prompt": prompt})
16 print("".join(str(chunk) for chunk in output) if not isinstance(output, str) else output)
AttributeTypeDescription
customer_identifierstrIdentifies the end user in Respan analytics.
thread_identifierstrGroups related messages into a conversation.
metadatadictCustom key-value pairs. Merged with default metadata.