@@ -123,6 +123,136 @@ await browser.close();
123123
124124---
125125
126+ ## Agent Execution Tracing (NEW in v0.3.1)
127+
128+ Record complete agent execution traces for debugging, analysis, and replay. Traces capture every step, snapshot, LLM decision, and action in a structured JSONL format.
129+
130+ ### Quick Start: Agent with Tracing
131+
132+ ``` typescript
133+ import {
134+ SentienceBrowser ,
135+ SentienceAgent ,
136+ OpenAIProvider ,
137+ Tracer ,
138+ JsonlTraceSink
139+ } from ' sentience-ts' ;
140+ import { randomUUID } from ' crypto' ;
141+
142+ const browser = await SentienceBrowser .create ({ apiKey: process .env .SENTIENCE_API_KEY });
143+ const llm = new OpenAIProvider (process .env .OPENAI_API_KEY ! , ' gpt-4o' );
144+
145+ // Create a tracer
146+ const runId = randomUUID ();
147+ const sink = new JsonlTraceSink (` traces/${runId }.jsonl ` );
148+ const tracer = new Tracer (runId , sink );
149+
150+ // Create agent with tracer
151+ const agent = new SentienceAgent (browser , llm , 50 , true , tracer );
152+
153+ // Emit run_start
154+ tracer .emitRunStart (' SentienceAgent' , ' gpt-4o' );
155+
156+ try {
157+ await browser .getPage ().goto (' https://google.com' );
158+
159+ // Every action is automatically traced!
160+ await agent .act (' Click the search box' );
161+ await agent .act (" Type 'sentience ai' into the search field" );
162+ await agent .act (' Press Enter' );
163+
164+ tracer .emitRunEnd (3 );
165+ } finally {
166+ // Flush trace to disk
167+ await agent .closeTracer ();
168+ await browser .close ();
169+ }
170+
171+ console .log (` ✅ Trace saved to: traces/${runId }.jsonl ` );
172+ ```
173+
174+ ### What Gets Traced
175+
176+ Each agent action generates multiple events:
177+
178+ 1 . ** step_start** - Before action execution (goal, URL, attempt)
179+ 2 . ** snapshot** - Page state with all interactive elements
180+ 3 . ** llm_response** - LLM decision (model, tokens, response)
181+ 4 . ** action** - Executed action (type, element ID, success)
182+ 5 . ** error** - Any failures (error message, retry attempt)
183+
184+ ** Example trace output:**
185+ ``` jsonl
186+ {"v" :1 ,"type" :" run_start" ,"ts" :" 2025-12-26T10:00:00.000Z" ,"run_id" :" abc-123" ,"seq" :1 ,"data" :{"agent" :" SentienceAgent" ,"llm_model" :" gpt-4o" }}
187+ {"v" :1 ,"type" :" step_start" ,"ts" :" 2025-12-26T10:00:01.000Z" ,"run_id" :" abc-123" ,"seq" :2 ,"step_id" :" step-1" ,"data" :{"step_index" :1 ,"goal" :" Click the search box" ,"attempt" :0 ,"url" :" https://google.com" }}
188+ {"v" :1 ,"type" :" snapshot" ,"ts" :" 2025-12-26T10:00:01.500Z" ,"run_id" :" abc-123" ,"seq" :3 ,"step_id" :" step-1" ,"data" :{"url" :" https://google.com" ,"elements" :[... ]}}
189+ {"v" :1 ,"type" :" llm_response" ,"ts" :" 2025-12-26T10:00:02.000Z" ,"run_id" :" abc-123" ,"seq" :4 ,"step_id" :" step-1" ,"data" :{"model" :" gpt-4o" ,"prompt_tokens" :250 ,"completion_tokens" :10 ,"response_text" :" CLICK(42)" }}
190+ {"v" :1 ,"type" :" action" ,"ts" :" 2025-12-26T10:00:02.500Z" ,"run_id" :" abc-123" ,"seq" :5 ,"step_id" :" step-1" ,"data" :{"action_type" :" click" ,"element_id" :42 ,"success" :true }}
191+ {"v" :1 ,"type" :" run_end" ,"ts" :" 2025-12-26T10:00:03.000Z" ,"run_id" :" abc-123" ,"seq" :6 ,"data" :{"steps" :1 }}
192+ ```
193+
194+ ### Reading and Analyzing Traces
195+
196+ ``` typescript
197+ import * as fs from ' fs' ;
198+
199+ // Read trace file
200+ const content = fs .readFileSync (` traces/${runId }.jsonl ` , ' utf-8' );
201+ const events = content .trim ().split (' \n ' ).map (JSON .parse );
202+
203+ console .log (` Total events: ${events .length } ` );
204+
205+ // Analyze events
206+ events .forEach (event => {
207+ console .log (` [${event .seq }] ${event .type } - ${event .ts } ` );
208+ });
209+
210+ // Filter by type
211+ const actions = events .filter (e => e .type === ' action' );
212+ console .log (` Actions taken: ${actions .length } ` );
213+
214+ // Get token usage
215+ const llmEvents = events .filter (e => e .type === ' llm_response' );
216+ const totalTokens = llmEvents .reduce ((sum , e ) => sum + (e .data .prompt_tokens || 0 ) + (e .data .completion_tokens || 0 ), 0 );
217+ console .log (` Total tokens: ${totalTokens } ` );
218+ ```
219+
220+ ### Tracing Without Agent (Manual)
221+
222+ You can also use the tracer directly for custom workflows:
223+
224+ ``` typescript
225+ import { Tracer , JsonlTraceSink } from ' sentience-ts' ;
226+ import { randomUUID } from ' crypto' ;
227+
228+ const runId = randomUUID ();
229+ const sink = new JsonlTraceSink (` traces/${runId }.jsonl ` );
230+ const tracer = new Tracer (runId , sink );
231+
232+ // Emit custom events
233+ tracer .emit (' custom_event' , {
234+ message: ' Something happened' ,
235+ details: { foo: ' bar' }
236+ });
237+
238+ // Use convenience methods
239+ tracer .emitRunStart (' MyAgent' , ' gpt-4o' );
240+ tracer .emitStepStart (' step-1' , 1 , ' Do something' );
241+ tracer .emitError (' step-1' , ' Something went wrong' );
242+ tracer .emitRunEnd (1 );
243+
244+ // Flush to disk
245+ await tracer .close ();
246+ ```
247+
248+ ### Schema Compatibility
249+
250+ Traces are ** 100% compatible** with Python SDK traces - use the same tools to analyze traces from both TypeScript and Python agents!
251+
252+ ** See full example:** [ examples/agent-with-tracing.ts] ( examples/agent-with-tracing.ts )
253+
254+ ---
255+
126256## Agent Layer Examples
127257
128258### Google Search (6 lines of code)
0 commit comments