meta_query only check if both value are set

You can use isset to check if the values are set, and then pass them as arguments. Divide your query by 2 parts and use them as needed.

// Check if both are set 
if ( isset( $max_age ) && isset( $min_age ) ) {
    $meta_query = array(
        'relation' => 'AND',
        array(
          'key'     => 'min_age',
          'value'   => $min_age,
          'type' => 'numeric',
          'compare' => '>=',
        ),
        array(
          'key'     => 'max_age',
          'value'   => $max_age,
          'type' => 'numeric',
          'compare' => '<=',
        ),
        array(
          'key'     => 'max_age',
          'value'   => '',
          'compare' => '!=',
        )
    );
} else {
    $meta_query = array(
        array(
          'key'     => 'min_age',
          'value'   => $min_age,
          'type' => 'numeric',
          'compare' => '>=',
        )
    );
}
// Now set the query
$query->set( 'meta_query', $meta_query );

If both $min_age and $max_age are always set and you want to check if they are empty or not, you can use empty() instead of isset().