How to trace cost for AzureChatOpenAI with a custom model #28949
Replies: 1 comment 4 replies
-
To ensure accurate cost tracing for AzureChatOpenAI when using a custom model with structured output and a traceable decorator, you can follow these steps:
Here's an example of how you can implement these steps: from langchain_openai import AzureChatOpenAI
from pydantic import BaseModel, Field
class AnnotatedAnswer(BaseModel):
# Define your structured output model here
pass
@traceable(
run_type="llm",
metadata={"ls_provider": "azure", "ls_model_name": "azure_openai_4o"}
)
def annotate_answer(answer, docs) -> List[Citation]:
llm = AzureChatOpenAI(
azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"),
api_key=os.getenv("AZURE_OPENAI_API_KEY"),
api_version=os.getenv("AZURE_OPENAI_API_VERSION"),
streaming=True,
stream_usage=True, # Enable streaming usage
)
formatted_docs = format_docs(docs)
prompt = PromptTemplate(
template="""...""" # Your prompt template
)
chain = prompt | llm.with_structured_output(AnnotatedAnswer)
response = chain.invoke({"answer": answer, "context": formatted_docs})
# Access usage metadata for cost tracing
usage_metadata = response.usage_metadata
print(f"Token usage: {usage_metadata}")
return response By following these steps, you should be able to trace costs accurately, considering both the function's signature and the LLM's output tokens [1]. To continue talking to Dosu, mention @dosu. Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Other |
Beta Was this translation helpful? Give feedback.
-
I created a Langgraph graph which contains a node that call a function on one of my nodes that uses AzureChatOpenAI with a hosted OpenAI model on my Azure AI studio in a chain as defined below:
I had been tracing my runs on LangSmith by adding the LANGCHAIN_API_KEY and setting LANGCHAIN_TRACING_V2 to true. I get the full traces for my Langgraph chain.
My issue is that I don't get any information about the cost as the cost column is empty. So I did some research and I added a new model on Langsmith portal with the following params:
Then I added the traceabale decorator to my function. So now my function looks like this:
Now when I login to LangSmith I can see the cost for the node that calls this function.
My concern is about whether this is the correct way to do tracing for the AzureChatOpenAI, firstly because I use this Chat model with structured output. Secondly because I have another completion node that calls the same model but return a dictionary. I am wondering in the traceable decorator takes into consideration my function's signature and if it considers the output of the function the output tokens, thus it calculates completion cost for the entire output of the function not the actual output of the LLM.
What's the best way to do cost tracing in this case?
Beta Was this translation helpful? Give feedback.
All reactions