Counting Tokens and Estimating Costs When Calling LLMs in Java
How to count tokens locally with JTokkit, calculate costs with BigDecimal, block requests exceeding budget, and reconcile with actual usage from Spring AI.
One topic per article, explained step by step with runnable code — from Java and Spring Boot to AI, system design and DevOps.
How to count tokens locally with JTokkit, calculate costs with BigDecimal, block requests exceeding budget, and reconcile with actual usage from Spring AI.
Use thiserror to define clear error types for libraries, use anyhow to consolidate errors and add context for applications, and combine both.
Distinguish object declaration, object expression, and companion object in Kotlin, how they create singletons, calling from Java, and common pitfalls.
Use template literal types to generate key names, parse route parameters, and write type-safe event emitters, along with type testing with Vitest and pitfalls to avoid.
Configure Maven for Kotlin and Java interoperability, handle nullability, @JvmStatic, @JvmOverloads, and common Spring Boot issues.
How to share data between threads using Arc<Mutex<T>>, pass messages with mpsc channels, and avoid deadlock, poisoning, and other common mistakes.
Receive tokens from LLM API via Server-Sent Events using HttpClient and WebClient, then push to browsers, with error handling, timeouts, and tests.
Which checks the strict flag in tsconfig enables, what errors each check catches, which additional flags to enable besides strict, and how to migrate an old project to strict.
Build a REST API CRUD with Axum 0.8, including routing, extractors, shared state, error handling with IntoResponse, and testing handlers without opening network ports.
Use by lazy for thread-safe lazy initialization, use Delegates.observable and vetoable to monitor or block property value changes.
Understand how LLMs call application functions, from raw JSON to @Tool in Spring AI. Includes examples for order lookup, cancellation, and testing without calling the model.
Use keyof, typeof, and indexed access types to infer types from existing data, reduce code duplication, and let the compiler catch incorrect keys for you.
How to declare function types, pass and return lambdas, use function references, closures, lambdas with receivers, and inline in Kotlin.
When to use Box, Rc, or Arc in Rust: heap allocation, sharing ownership in a single thread and across multiple threads, along with Weak, RefCell, and Mutex.
How to force LLM to return JSON matching schema in Spring Boot: map to record with Spring AI, enable native JSON Schema, validate, retry and write tests.
How to configure tsconfig and package.json so your TypeScript library generates proper .d.ts files with complete types for users and can be tested with Vitest.
A guide to asynchronous programming in Rust with Tokio, covering async/await, spawning tasks, join!, select!, timeout, channels, spawn_blocking, and testing async code.
Setup Spring Boot 3 project with Kotlin, write REST API, JPA entity, configuration and test, including common errors when migrating from Java.
Practical prompt engineering techniques like system prompt, delimiter, few-shot, structured output, and temperature, illustrated with Spring AI.
How to type async functions, using Promise<T>, Awaited, Promise.all, safe error handling, and avoiding common pitfalls with async callbacks.