Skip to content

Instantly share code, notes, and snippets.

@morganmcg1
Created July 17, 2025 22:48
Show Gist options
  • Save morganmcg1/e996c319527a493f176a293628016816 to your computer and use it in GitHub Desktop.
Save morganmcg1/e996c319527a493f176a293628016816 to your computer and use it in GitHub Desktop.
import os
import weave
from langchain_google_genai import ChatGoogleGenerativeAI
os.environ["GOOGLE_API_KEY"] =
GEMINI_MODEL_NAME = 'gemini-2.5-flash'
CUSTOM_MODEL_NAME = 'lc_' + GEMINI_MODEL_NAME
weave_client = weave.init("cost-testing")
weave_client.add_cost(
llm_id=CUSTOM_MODEL_NAME,
prompt_token_cost=0.3,
completion_token_cost=2.5
)
llm = ChatGoogleGenerativeAI(
model=GEMINI_MODEL_NAME,
)
@weave.op
def hack_usage(usage):
return usage
@weave.op
def a_great_model(messages):
response = llm.invoke(messages)
input_tokens = response.usage_metadata["input_tokens"]
output_tokens = response.usage_metadata["output_tokens"]
hack_usage({"usage": {
"prompt_tokens": int(input_tokens),
"completion_tokens": int(output_tokens),
"total_tokens": int(input_tokens + output_tokens),
},
"model": CUSTOM_MODEL_NAME})
return response
messages = [
("system", "You love long poems",),
("human", "tell me an incredibly short poem about bees."),
]
a_great_model(messages)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment