instrument-existing-agent-with-prefactor-sdk
Instrument Existing Agent With Prefactor SDK
Instrument a working agent that was built without Prefactor.
Core principle: instrument boundaries, not business logic.
Quick Start
- Bootstrap Prefactor resources with
skills/bootstrap-existing-agent-with-prefactor-cli/SKILL.md. - Install required Prefactor packages with the project's existing package manager (
bun,npm,pnpm, oryarn). - Identify runtime path: built-in adapter (
@prefactor/langchain,@prefactor/ai,@prefactor/openclaw) or custom@prefactor/coreadapter. - Add one top-level run span and child spans around LLM/tool boundaries.
- Preserve context propagation and package-prefixed span types.
- Record error metadata and rethrow original errors.
- Finish spans on success, error, cancel, and stream terminal paths.
- Verify in your project's build/test/typecheck flow.
Prerequisite
Before instrumentation, ensure these runtime credentials are set from CLI bootstrap output:
PREFACTOR_API_URLPREFACTOR_API_TOKENPREFACTOR_AGENT_ID
Use the created agent_id for PREFACTOR_AGENT_ID.
Also ensure dependencies are installed from npm via the project's package manager:
- one of
@prefactor/langchain,@prefactor/ai,@prefactor/openclaw @prefactor/coreonly when no built-in adapter is available
Coding Tool Trigger Phrases
If the user asks for any of these, apply this skill:
- "instrument this existing agent"
- "this agent already works, add prefactor tracing"
- "wrap this existing langchain/ai agent with prefactor"
- "add tracing for tool calls and runs"
- "tool calls are missing in my coding tool timeline"
Use With Custom Provider Skill
Sometimes you need both skills.
- If the framework/provider is already supported by a Prefactor adapter, use this skill directly.
- If the framework/provider is not supported yet, first use
skills/create-provider-package-with-core/SKILL.mdto build a custom adapter, then use this skill to instrument the existing agent with that adapter.
Recommended sequence when unsupported:
- Create provider adapter with
@prefactor/core. - Integrate adapter into the existing agent entrypoint.
- Validate run/llm/tool/error spans in real executions.
Implementation Rules
- Prefer built-in adapters before low-level
@prefactor/core:- LangChain ->
@prefactor/langchain - AI SDK ->
@prefactor/ai - OpenClaw ->
@prefactor/openclaw
- LangChain ->
- Import
init,withSpan, andshutdownfrom the same selected adapter package (@prefactor/aior@prefactor/langchain) when an adapter exists. - Do not mix adapter
initwithwithSpan/shutdownimported from@prefactor/coreunless you pass an explicit tracer towithSpan. - Why: mixed helper imports can lead to
No active tracer found(for example with nested package instances), which drops manual custom spans. - Keep imports package-local and explicit, for example:
import { init, withSpan, shutdown } from '@prefactor/ai';
// or
import { init, withSpan, shutdown } from '@prefactor/langchain';
- This mixed-import tracer risk applies to adapter-style flows (
@prefactor/ai,@prefactor/langchain) and not to the@prefactor/openclawplugin runtime model. - If a built-in adapter does not exist, follow
skills/create-provider-package-with-core/SKILL.md. - Keep provider span types package-prefixed (
langchain:*,ai-sdk:*,openclaw:*). - Run nested work inside active context so parent/child trace trees stay intact.
- Capture input/output safely (redact secrets, enforce truncation limits).
- Instrumentation must never crash user code.
Verification
Run equivalent project verification commands (for example build, typecheck, and tests).
Also run at least one real agent request and confirm:
- top-level run span exists
- child llm/tool spans are correctly nested
- terminal status appears for success and failure
References
- For coding-tool-oriented keyword coverage and trigger wording, read
references/coding-tool-triggers.md. - For an execution checklist and failure diagnostics, read
references/instrumentation-checklist.md.
Common Mistakes
- Instrumenting every helper instead of boundaries.
- Using generic span types.
- Swallowing exceptions after logging.
- Missing stream cancel/error completion paths.
More from prefactordev/typescript-sdk
prefactor-skill-selector
Use when choosing which Prefactor SDK skill to load for agent instrumentation or for building a custom provider integration on top of @prefactor/core.
37create-provider-package-with-core
Use when building a custom provider integration on top of @prefactor/core so your app can instrument agent, llm, and tool workflows without relying on a prebuilt adapter package.
35bootstrap-existing-agent-with-prefactor-cli
Use when an existing agent needs Prefactor resources created via the Prefactor CLI before SDK instrumentation is added.
32report-agent-risk-data
Use when an agent is already instrumented with Prefactor and you need to populate data_risk fields on its span types to enable compliance tracking and data governance.
5ai-sdk
Answer questions about the AI SDK and help build AI-powered features. Use when developers: (1) Ask about AI SDK functions like generateText, streamText, ToolLoopAgent, embed, or tools, (2) Want to build AI agents, chatbots, RAG systems, or text generation features, (3) Have questions about AI providers (OpenAI, Anthropic, Google, etc.), streaming, tool calling, structured output, or embeddings, (4) Use React hooks like useChat or useCompletion. Triggers on: "AI SDK", "Vercel AI SDK", "generateText", "streamText", "add AI to my app", "build an agent", "tool calling", "structured output", "useChat".
2caveman
>
1