AgentScope (tracing)

AgentScope is a Python framework for building agent applications with model abstractions, messages, and toolkits. Respan patches AgentScope agent, chat model, and toolkit surfaces so agent replies, model calls, tool calls, streaming output, and failures appear as Respan traces.

This tracing setup uses your model provider credentials directly. To route model calls through Respan instead, use the AgentScope gateway setup.

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 AgentScope gateway setup to route model calls through the Respan gateway.

Setup

1

Install packages

$pip install respan-ai respan-instrumentation-agentscope agentscope openai
2

Set environment variables

$export OPENAI_API_KEY="YOUR_OPENAI_API_KEY"
$export RESPAN_API_KEY="YOUR_RESPAN_API_KEY"

OPENAI_API_KEY is used by AgentScope for direct model requests. RESPAN_API_KEY exports traces to Respan.

3

Initialize and run

1import asyncio
2import os
3
4from agentscope.agent import Agent
5from agentscope.credential import OpenAICredential
6from agentscope.message import UserMsg
7from agentscope.model import OpenAIChatModel
8from respan import Respan, workflow
9from respan_instrumentation_agentscope import AgentScopeInstrumentor
10
11credential = OpenAICredential(api_key=os.environ["OPENAI_API_KEY"])
12model = OpenAIChatModel(
13 credential=credential,
14 model="gpt-4o-mini",
15 stream=False,
16)
17
18respan = Respan(
19 app_name="agentscope-demo",
20 instrumentations=[AgentScopeInstrumentor(model=model)],
21)
22
23agent = Agent(
24 name="PlanningAgent",
25 system_prompt="Return concise planning updates.",
26 model=model,
27)
28
29@workflow(name="agentscope_agent_reply")
30async def run_agent() -> str:
31 result = await agent.reply(UserMsg(name="user", content="Draft a tiny plan."))
32 return result.get_text_content()
33
34async def main() -> None:
35 print(await run_agent())
36
37asyncio.run(main())
38respan.flush()
39respan.shutdown()
4

View your trace

Open the Traces page to see agent, model, and tool spans in the same trace tree.

Configuration

ParameterTypeDefaultDescription
api_keystr | NoneRESPAN_API_KEYRespan API key used to export traces.
base_urlstr | NoneRESPAN_BASE_URLOptional Respan trace export API URL.
instrumentationslist[]Include AgentScopeInstrumentor() or targeted model/toolkit instrumentors.
customer_identifierstr | NoneNoneDefault customer identifier for all spans.
metadatadict | NoneNoneDefault metadata attached to all spans.
environmentstr | NoneNoneEnvironment tag, for example "production".

What gets traced

AgentScope operationRespan spanNotes
Agent.reply() / reply_stream()agentCaptures agent name, input, final output, and streaming output after consumption.
Chat model callschatCaptures messages, completion, model name, tool definitions, tool calls, and token usage when available.
Toolkit call_tool()toolCaptures tool name, arguments, result, and failures.

Attributes

Set defaults at initialization. These apply to every span emitted by the AgentScope instrumentor.

1from respan import Respan
2from respan_instrumentation_agentscope import AgentScopeInstrumentor
3
4respan = Respan(
5 instrumentations=[AgentScopeInstrumentor(model=model)],
6 customer_identifier="user_123",
7 metadata={"service": "agentscope-api", "version": "1.0.0"},
8 environment="production",
9)

Use targeted AgentScopeInstrumentor(model=model) instances for custom or externally defined AgentScope-compatible models that are not reachable through the default AgentScope module patching path.