@@ -2128,8 +2128,8 @@ def test_agent_action_and_finish_imports():
21282128 assert call_args ["event" ] == "$ai_span"
21292129
21302130
2131- def test_billable_field_in_generation_metadata (mock_client ):
2132- """Test that the billable field is properly stored in GenerationMetadata."""
2131+ def test_custom_metadata_field_in_generation_metadata (mock_client ):
2132+ """Test that custom_metadata is properly stored in GenerationMetadata."""
21332133 callbacks = CallbackHandler (mock_client )
21342134 run_id = uuid .uuid4 ()
21352135
@@ -2143,7 +2143,7 @@ def test_billable_field_in_generation_metadata(mock_client):
21432143 metadata = {
21442144 "ls_model_name" : "gpt-4o" ,
21452145 "ls_provider" : "openai" ,
2146- "posthog_ai_billable " : True ,
2146+ "custom_metadata " : { "$ai_billable" : True } ,
21472147 },
21482148 name = "test" ,
21492149 )
@@ -2156,11 +2156,11 @@ def test_billable_field_in_generation_metadata(mock_client):
21562156 provider = "openai" ,
21572157 base_url = "https://api.openai.com" ,
21582158 name = "test" ,
2159- billable = True ,
2159+ custom_metadata = { "$ai_billable" : True } ,
21602160 end_time = None ,
21612161 )
21622162 assert callbacks ._runs [run_id ] == expected
2163- assert callbacks ._runs [run_id ].billable is True
2163+ assert callbacks ._runs [run_id ].custom_metadata == { "$ai_billable" : True }
21642164
21652165 callbacks ._pop_run_metadata (run_id )
21662166
@@ -2175,15 +2175,15 @@ def test_billable_field_in_generation_metadata(mock_client):
21752175 metadata = {
21762176 "ls_model_name" : "gpt-4o" ,
21772177 "ls_provider" : "openai" ,
2178- "posthog_ai_billable " : False ,
2178+ "custom_metadata " : { "$ai_billable" : False } ,
21792179 },
21802180 name = "test" ,
21812181 )
21822182
2183- assert callbacks ._runs [run_id2 ].billable is False
2183+ assert callbacks ._runs [run_id2 ].custom_metadata == { "$ai_billable" : False }
21842184 callbacks ._pop_run_metadata (run_id2 )
21852185
2186- # Test default billable=False when not provided
2186+ # Test when custom_metadata not provided
21872187 run_id3 = uuid .uuid4 ()
21882188 with patch ("time.time" , return_value = 1234567890 ):
21892189 callbacks ._set_llm_metadata (
@@ -2195,7 +2195,7 @@ def test_billable_field_in_generation_metadata(mock_client):
21952195 name = "test" ,
21962196 )
21972197
2198- assert callbacks ._runs [run_id3 ].billable is False
2198+ assert callbacks ._runs [run_id3 ].custom_metadata is None
21992199
22002200
22012201def test_billable_property_in_generation_event (mock_client ):
@@ -2210,7 +2210,10 @@ def test_billable_property_in_generation_event(mock_client):
22102210 {},
22112211 run_id ,
22122212 messages = [{"role" : "user" , "content" : "Test" }],
2213- metadata = {"posthog_ai_billable" : True , "ls_model_name" : "test-model" },
2213+ metadata = {
2214+ "custom_metadata" : {"$ai_billable" : True },
2215+ "ls_model_name" : "test-model" ,
2216+ },
22142217 invocation_params = {},
22152218 )
22162219
@@ -2237,7 +2240,7 @@ def test_billable_property_in_generation_event(mock_client):
22372240
22382241
22392242def test_billable_defaults_to_false_in_event (mock_client ):
2240- """Test that $ai_billable defaults to False when not specified."""
2243+ """Test that $ai_billable is not present when not specified."""
22412244 prompt = ChatPromptTemplate .from_messages ([("user" , "Test query" )])
22422245 model = FakeMessagesListChatModel (
22432246 responses = [AIMessage (content = "Test response" )],
@@ -2255,7 +2258,7 @@ def test_billable_defaults_to_false_in_event(mock_client):
22552258
22562259 assert generation_call is not None
22572260 props = generation_call [1 ]["properties" ]
2258- assert props [ "$ai_billable" ] is False
2261+ assert "$ai_billable" not in props
22592262
22602263
22612264def test_billable_with_real_chain (mock_client ):
@@ -2271,12 +2274,12 @@ def test_billable_with_real_chain(mock_client):
22712274 metadata = {
22722275 "ls_model_name" : "fake-model" ,
22732276 "ls_provider" : "fake" ,
2274- "posthog_ai_billable " : True ,
2277+ "custom_metadata " : { "$ai_billable" : True } ,
22752278 },
22762279 invocation_params = {"temperature" : 0.7 },
22772280 )
22782281
2279- assert callbacks ._runs [run_id ].billable is True
2282+ assert callbacks ._runs [run_id ].custom_metadata == { "$ai_billable" : True }
22802283
22812284 mock_response = MagicMock ()
22822285 mock_response .generations = [[MagicMock ()]]
0 commit comments