If your scene has only opaque objects, this post explains the fastest method for shadow rays with example code:
https://forums.developer.nvidia.com/t/anyhit-program-as-shadow-ray-with-optix-7-2/181312/2
If you have cutout opacity the same post points to example code for anyhit programs handling opaque and cutout opacity materials.
Don’t use anyhit programs for visibility tests when you’re only handling opaque materials! Note that opaque is meant in the sense of no-cutout and includes transparent materials as well because shadows from that are lit by caustics and those get handled by the light transport algorithms, not by the visibility test rays. A global illumination path tracer only needs boolean visibility tests.
Further down it describes where to find example code inside the SDK attenuating transmissions for shadows in non-global illumation light transports.
OptiX forum search for “shadow ray” topics: Search results for 'shadow ray #visualization:optix' - NVIDIA Developer Forums