Skip to navigation

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

import asyncio
import os
from agentscope.agent import Agent
from agentscope.credential import OpenAICredential
from agentscope.message import UserMsg
from agentscope.model import OpenAIChatModel
from respan import Respan, workflow
from respan_instrumentation_agentscope import AgentScopeInstrumentor
credential = OpenAICredential(api_key=os.environ["OPENAI_API_KEY"])
model = OpenAIChatModel(
credential=credential,
model="gpt-4o-mini",
stream=False,
)
respan = Respan(
app_name="agentscope-demo",
instrumentations=[AgentScopeInstrumentor(model=model)],
)
agent = Agent(
name="PlanningAgent",
system_prompt="Return concise planning updates.",
model=model,
)
@workflow(name="agentscope_agent_reply")
async def run_agent() -> str:
result = await agent.reply(UserMsg(name="user", content="Draft a tiny plan."))
return result.get_text_content()
async def main() -> None:
print(await run_agent())
asyncio.run(main())
respan.flush()
respan.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.

from respan import Respan
from respan_instrumentation_agentscope import AgentScopeInstrumentor
respan = Respan(
instrumentations=[AgentScopeInstrumentor(model=model)],
customer_identifier="user_123",
metadata={"service": "agentscope-api", "version": "1.0.0"},
environment="production",
)

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.