@@ -116,12 +116,17 @@ def call_llm_and_track_usage(
116116 error = None
117117 http_status = 200
118118 usage : Dict [str , Any ] = {}
119+ error_params : Dict [str , any ] = {}
119120
120121 try :
121122 response = call_method (** kwargs )
122123 except Exception as exc :
123124 error = exc
124125 http_status = getattr (exc , "status_code" , 0 ) # default to 0 becuase its likely an SDK error
126+ error_params = {
127+ "$ai_is_error" : True ,
128+ "$ai_error" : exc .__str__ (),
129+ }
125130 finally :
126131 end_time = time .time ()
127132 latency = end_time - start_time
@@ -149,6 +154,7 @@ def call_llm_and_track_usage(
149154 "$ai_trace_id" : posthog_trace_id ,
150155 "$ai_base_url" : str (base_url ),
151156 ** (posthog_properties or {}),
157+ ** (error_params or {}),
152158 }
153159
154160 if posthog_distinct_id is None :
@@ -186,12 +192,17 @@ async def call_llm_and_track_usage_async(
186192 error = None
187193 http_status = 200
188194 usage : Dict [str , Any ] = {}
195+ error_params : Dict [str , any ] = {}
189196
190197 try :
191198 response = await call_async_method (** kwargs )
192199 except Exception as exc :
193200 error = exc
194201 http_status = getattr (exc , "status_code" , 0 ) # default to 0 because its likely an SDK error
202+ error_params = {
203+ "$ai_is_error" : True ,
204+ "$ai_error" : exc .__str__ (),
205+ }
195206 finally :
196207 end_time = time .time ()
197208 latency = end_time - start_time
@@ -219,6 +230,7 @@ async def call_llm_and_track_usage_async(
219230 "$ai_trace_id" : posthog_trace_id ,
220231 "$ai_base_url" : str (base_url ),
221232 ** (posthog_properties or {}),
233+ ** (error_params or {}),
222234 }
223235
224236 if posthog_distinct_id is None :
0 commit comments