I’ve been reading through the Lucee documentation for all the recent AI-based additions which have been added to the language over the last year or so, but I’m not seeing anything about how logging/tracking of the calls to the AI providers can be implemented.
We’re currently using a small custom framework we have been using for our AI integrations, because part of our requirement is that we can log the prompts sent, responses received, and with the prompt/response token counts (so we can verify billing/throttle customers based on prompt usage).
Maybe I’m missing it, but are there any hooks with the current AI functionality which would allow us to add some tracking/logging to the native AI functionality so we could capture this information?
We are getting ready to implement MCP support for our application, so thought w/Lucee’s planned support for MCP it would make sense to look into migrating to Lucee’s native AI functionality, but the lack of support for being able to track this stuff is prohibitive for us.
Am I missing something? Are there some ways we might be able to implement this tracking/logging?
1 Like
Hi @dswitzer,
To our current knowledge there are no plans to change anything here, because in our opinion the existing ai log already covers this. Set it to DEBUG level and Lucee logs every call to the provider (OpenAI-compatible, Claude and Gemini engines): the full JSON request it sends, including your prompt, and the raw response it gets back, including streamed chunks. The provider’s response normally contains its usage/token counts. If no ai log is defined, the entries go to the application log instead.
You can configure that log however you like, in the Lucee admin or in the logging section of .CFConfig.json. Pick the appender and layout you need (file, console, datasource, or your own appender that feeds your log aggregation), so you can send the data wherever you want to process it, e.g. for billing or throttling.
If you still feel something is missing, please give us concrete details along the lines of “when I do X, I expect to see Y in the log”, with a small code example. Then we can look at it and possibly turn it into a ticket.
Our corporate policy is that we must carefully track AI costs, which means we need to be able to track token usage by tenant.
While I haven’t dug into the AI logs that Lucee generates, my guess is they’d be rather difficult to parse into realtime cost analysis. We also have safe guards in place to prevent tenants from abusing AI usage (so we don’t get hit with w/any unexpected bills).
Without a way to hook into the Lucee AI functionality to implementing our tracking/safe guards, its a feature we can’t use.
I’d assume we’re not alone in this approach, but perhaps we are in some kind of minority (I would hope not though).
actually i think that is something worth adding as a feature to our implementation. a proper way to keep track on token usage.
i think it is not that easy because every interface is different, even the implementations that use OpenAI ar very differen from each other when it comes to edge cases, we will dig into this.
Thanks for the input — we’ve filed two tickets for this:
- LDEV-6527 — AI: expose token usage via a new
AIGetUsage(session) function and log a usage line per call. Purely additive, no change to existing AI APIs.
- LDEV-6528 — AI: optional per-connection
maxTokensPerSession / maxTokensPerRequest limits.
Feedback welcome on the tickets.
Looks great! I appreciate you taking the time to think about my request.
I wonder if it would make more sense to just expose a event listener you could attach to your AI sessions that might have methods like:
onBeforePromptSent — Gets the prompt that would be sent. Allows you to override the prompt or cancel the request.
onResponse — Gets the raw response returned by the AI API. Allows you to override/inspect the raw response details.
onStreamingResponse — Gets the raw results of a streaming response. Allows you to override/abort the HTTP response connection.
This would allow for a little more control, especially in situations where new AI providers are added that Lucee might not initially support.
This would also give you the ability to add some prompt guarding infrastructure, to prevent things like prompt injections.
1 Like
a listener always is a good thing and flexibility is king with that interface.
I think you pointed out a important issue with the current implementation, so we will address for sure asap.
2 Likes
@dswitzer we’ve filed your listener idea as LDEV-6540 for Lucee 8.0: Jira