@@ -113,6 +113,7 @@ def test_metadata_capture(mock_client):
113113 base_url = "https://us.posthog.com" ,
114114 name = "test" ,
115115 end_time = None ,
116+ posthog_properties = None ,
116117 )
117118 assert callbacks ._runs [run_id ] == expected
118119 with patch ("time.time" , return_value = 1234567891 ):
@@ -1269,6 +1270,7 @@ def test_metadata_tools(mock_client):
12691270 name = "test" ,
12701271 tools = tools ,
12711272 end_time = None ,
1273+ posthog_properties = None ,
12721274 )
12731275 assert callbacks ._runs [run_id ] == expected
12741276 with patch ("time.time" , return_value = 1234567891 ):
@@ -2128,8 +2130,8 @@ def test_agent_action_and_finish_imports():
21282130 assert call_args ["event" ] == "$ai_span"
21292131
21302132
2131- def test_billable_field_in_generation_metadata (mock_client ):
2132- """Test that the billable field is properly stored in GenerationMetadata."""
2133+ def test_custom_metadata_field_in_generation_metadata (mock_client ):
2134+ """Test that posthog_properties is properly stored in GenerationMetadata."""
21332135 callbacks = CallbackHandler (mock_client )
21342136 run_id = uuid .uuid4 ()
21352137
@@ -2143,7 +2145,7 @@ def test_billable_field_in_generation_metadata(mock_client):
21432145 metadata = {
21442146 "ls_model_name" : "gpt-4o" ,
21452147 "ls_provider" : "openai" ,
2146- "posthog_ai_billable " : True ,
2148+ "posthog_properties " : { "$ai_billable" : True } ,
21472149 },
21482150 name = "test" ,
21492151 )
@@ -2156,11 +2158,11 @@ def test_billable_field_in_generation_metadata(mock_client):
21562158 provider = "openai" ,
21572159 base_url = "https://api.openai.com" ,
21582160 name = "test" ,
2159- billable = True ,
2161+ posthog_properties = { "$ai_billable" : True } ,
21602162 end_time = None ,
21612163 )
21622164 assert callbacks ._runs [run_id ] == expected
2163- assert callbacks ._runs [run_id ].billable is True
2165+ assert callbacks ._runs [run_id ].posthog_properties == { "$ai_billable" : True }
21642166
21652167 callbacks ._pop_run_metadata (run_id )
21662168
@@ -2175,15 +2177,15 @@ def test_billable_field_in_generation_metadata(mock_client):
21752177 metadata = {
21762178 "ls_model_name" : "gpt-4o" ,
21772179 "ls_provider" : "openai" ,
2178- "posthog_ai_billable " : False ,
2180+ "posthog_properties " : { "$ai_billable" : False } ,
21792181 },
21802182 name = "test" ,
21812183 )
21822184
2183- assert callbacks ._runs [run_id2 ].billable is False
2185+ assert callbacks ._runs [run_id2 ].posthog_properties == { "$ai_billable" : False }
21842186 callbacks ._pop_run_metadata (run_id2 )
21852187
2186- # Test default billable=False when not provided
2188+ # Test when posthog_properties not provided
21872189 run_id3 = uuid .uuid4 ()
21882190 with patch ("time.time" , return_value = 1234567890 ):
21892191 callbacks ._set_llm_metadata (
@@ -2195,7 +2197,7 @@ def test_billable_field_in_generation_metadata(mock_client):
21952197 name = "test" ,
21962198 )
21972199
2198- assert callbacks ._runs [run_id3 ].billable is False
2200+ assert callbacks ._runs [run_id3 ].posthog_properties is None
21992201
22002202
22012203def test_billable_property_in_generation_event (mock_client ):
@@ -2210,7 +2212,10 @@ def test_billable_property_in_generation_event(mock_client):
22102212 {},
22112213 run_id ,
22122214 messages = [{"role" : "user" , "content" : "Test" }],
2213- metadata = {"posthog_ai_billable" : True , "ls_model_name" : "test-model" },
2215+ metadata = {
2216+ "posthog_properties" : {"$ai_billable" : True },
2217+ "ls_model_name" : "test-model" ,
2218+ },
22142219 invocation_params = {},
22152220 )
22162221
@@ -2237,7 +2242,7 @@ def test_billable_property_in_generation_event(mock_client):
22372242
22382243
22392244def test_billable_defaults_to_false_in_event (mock_client ):
2240- """Test that $ai_billable defaults to False when not specified."""
2245+ """Test that $ai_billable is not present when not specified."""
22412246 prompt = ChatPromptTemplate .from_messages ([("user" , "Test query" )])
22422247 model = FakeMessagesListChatModel (
22432248 responses = [AIMessage (content = "Test response" )],
@@ -2255,7 +2260,7 @@ def test_billable_defaults_to_false_in_event(mock_client):
22552260
22562261 assert generation_call is not None
22572262 props = generation_call [1 ]["properties" ]
2258- assert props [ "$ai_billable" ] is False
2263+ assert "$ai_billable" not in props
22592264
22602265
22612266def test_billable_with_real_chain (mock_client ):
@@ -2271,12 +2276,12 @@ def test_billable_with_real_chain(mock_client):
22712276 metadata = {
22722277 "ls_model_name" : "fake-model" ,
22732278 "ls_provider" : "fake" ,
2274- "posthog_ai_billable " : True ,
2279+ "posthog_properties " : { "$ai_billable" : True } ,
22752280 },
22762281 invocation_params = {"temperature" : 0.7 },
22772282 )
22782283
2279- assert callbacks ._runs [run_id ].billable is True
2284+ assert callbacks ._runs [run_id ].posthog_properties == { "$ai_billable" : True }
22802285
22812286 mock_response = MagicMock ()
22822287 mock_response .generations = [[MagicMock ()]]
0 commit comments