Writer (tracing)

Writer is an AI content platform with models for text, vision, translation, and knowledge graph workflows.

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

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

Setup

1

Install packages

$pip install respan-ai respan-instrumentation-writer writerai
2

Set environment variables

$export RESPAN_API_KEY="YOUR_RESPAN_API_KEY"
$export WRITER_API_KEY="YOUR_WRITER_API_KEY"

Optional:

$export RESPAN_BASE_URL="https://api.respan.ai/api"
$export WRITER_MODEL="palmyra-x5"
3

Initialize and run

1import os
2
3from respan import Respan, workflow
4from respan_instrumentation_writer import WriterInstrumentor
5from writerai import Writer
6
7respan = Respan(
8 api_key=os.environ["RESPAN_API_KEY"],
9 base_url=os.getenv("RESPAN_BASE_URL", "https://api.respan.ai/api"),
10 instrumentations=[WriterInstrumentor()],
11)
12
13client = Writer(api_key=os.environ["WRITER_API_KEY"])
14
15
16@workflow(name="writer_chat.workflow")
17def run_writer_chat() -> str:
18 completion = client.chat.chat(
19 model=os.getenv("WRITER_MODEL", "palmyra-x5"),
20 messages=[{"role": "user", "content": "Write one line about tracing."}],
21 max_tokens=64,
22 )
23 return completion.choices[0].message.content
24
25
26print(run_writer_chat())
27respan.flush()
28respan.shutdown()
4

View your trace

Open the Traces page and search for workflow name writer_chat.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. WriterInstrumentor()).
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_writer import WriterInstrumentor
3
4respan = Respan(
5 instrumentations=[WriterInstrumentor()],
6 customer_identifier="user_123",
7 metadata={"service": "writer-api", "version": "1.0.0"},
8)

With propagate_attributes

1import os
2
3from respan import Respan, propagate_attributes
4from respan_instrumentation_writer import WriterInstrumentor
5from writerai import Writer
6
7respan = Respan(instrumentations=[WriterInstrumentor()])
8
9
10client = Writer(api_key=os.environ.get("WRITER_API_KEY", "local-mock-token"))
11
12
13def run_writer_with_attributes(user_id: str):
14 with propagate_attributes(
15 customer_identifier=user_id,
16 thread_identifier="writer_thread",
17 metadata={"plan": "enterprise"},
18 ):
19 completion = client.chat.chat(
20 model=os.getenv("WRITER_MODEL", "palmyra-x5"),
21 messages=[{"role": "user", "content": "Write one sentence for trace demos."}],
22 max_tokens=32,
23 )
24 print(completion.choices[0].message.content)
AttributeTypeDescription
customer_identifierstrIdentifies the end user in Respan analytics.
thread_identifierstrGroups related messages into a conversation.
metadatadictCustom key-value pairs. Merged with default metadata.